2

After selecting dates from DateField's form in django and hitting submit button, is_valid() succeeds, but cleaned_data shows None. Does anyone know what is the issue? Thanks

forms.py

class DateForm(forms.Form):
    day_from = forms.DateField(label=u'Začiatok obdobia', input_formats=['%d/%m/%Y'], 
        required=False, widget=forms.DateInput(format='%d/%m/%Y'))
    day_to = forms.DateField(label=u'Koniec obdobia', input_formats=['%d/%m/%Y'], 
        required=False, widget=forms.DateInput(format='%d/%m/%Y'))

views.py

def dp_list(request):
if request.method == "POST":
    form_date = DateForm(request.POST)
    if form_date.is_valid():
        date_from = form_date.cleaned_data['day_from']
        date_to = form_date.cleaned_data['day_to']
        print(date_from, date_to, type(date_from), type(date_to))
        print(form_date.cleaned_data)

datelist.html

<form action="" method="post">
     {% csrf_token %}
     {{ form | materializecss:"s4"}}
     <span class="align-bottom">
         <input class="btn primary-btn pink" type="submit" value="Zobraz">
     </span>
</form>

Output of prints in console:

> ## None None class 'NoneType' class 'NoneType'
> ## {'day_from': None, 'day_to': None}
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
Peter
  • 21
  • 1

1 Answers1

0

This happens because one of the fields isn't cleaned. You can confirm which field by doing an else and print(form_date.cleaned_data), the print's should have one less field.

TheEagle
  • 5,808
  • 3
  • 11
  • 39
Shahzar Ali
  • 181
  • 1
  • 2