简体   繁体   中英

Django DateField form generates None in cleaned_data

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}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM