ValidationError when submitting a form with custom date format

Viewed 278

When submitting a django form i am getting a ValidationError.

In my form this is my input: 01/01/2017 But django tell me that the format must be 'AAAA-MM-GG'.
With this Exception Location: /usr/local/lib/python3.5/dist-packages/django/db/models/fields/init.py in to_python, line 1271

This is the code on my project:

# models.py
class Archivio(models.Model):  
    sys_codice = models.AutoField(primary_key=True)
    some_other_field ...

class QuoteIscrizione(models.Model):  
    sys_chiave = models.AutoField(primary_key=True)
    sys_cod = models.ForeignKey(Archivio, on_delete=models.CASCADE)
    data_quota = models.DateField(blank=True, null=True)  


# forms.py
class ArchivioSearchForm(forms.Form):  
    data_quota = forms.DateField(widget=forms.DateInput(format='%d/%m/%Y'), input_formats=('%d/%m/%Y',), required=False)  

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for field in self:
            field.field.widget.attrs['class'] = 'form-control '
                if type(field.field.widget) is DateInput:
                    field.field.widget.attrs['class'] += 'datepicker'  
    def clean(self):
        data_quota = self.cleaned_data['data_quota']  
        return self.cleaned_data

# views.py  
def ArchivioSearchView(request):  
    model = Archivio
    form = ArchivioSearchForm()   

    if request.GET.get('data_quota'):  
        selection = Archivio.objects.filter(Q(quoteiscrizione__data_quota__gte=request.GET.get('data_quota'))  
        return render(request, 'search_template.html', {'selection':selection, 'form':form})  

# search_template.html  
<form action="" method="get" class="form-horizontal">
    <div class="form-group form-group-md">            
    <input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">
        {% for field in form %}
            <div class="form-group row">
                <label class="col-md-3 col-form-label" for="{{ field.id_for_label }}">{{ field.label }}</label>
                <div class="col-md-6">
                    {{ field.as_widget() }}
                </div>
            </div>
        {% endfor %}
    <input type="submit" value="Confermo" />  
    </div>
</form>
{% for select in selection %}
    <h4><li><a href="{{ url('archivio_detail', args=[select.pk]) }}">{{ select.nominativo }}</a></li></h4>
{% endfor %}

And I also have this on my settings.py

LANGUAGE_CODE = 'it-IT'  
TIME_ZONE = 'Europe/Rome'  
USE_I18N = True
USE_L10N = True  
USE_TZ = True

I have tried to use on my settings.py of course after turning USE_L10N = False DATE_INPUT_FORMATS = ['%d/%m/%Y']

I have tried also to use on my forms.py on init function on for loop:

if type(field.field.widget) is DateInput:  
    field.field.widget.input_formats = ['d%/%m/%Y']  

I have tried not only with Lists but also with strings like this '%d/%m/%Y'.

I also see that this error occur even if my input is 2017/01/01!

I cannot figure out what I am doing wrong. Django continues expecting dates in format 'YYYY-MM-DD' even if I override every possible method or parameter of DateField's widget.

Thank to all of you that will take some of your time trying to help me!

1 Answers
Related