Django DateField表单在cleaning_data中生成None

时间:2016-12-12 10:07:12

标签: python django django-forms datefield

从django中的DateField表单中选择日期并点击提交按钮后,is_valid()成功,但cleaning_data显示无。 有谁知道这是什么问题?感谢

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>

在控制台中输出打印件:

> ## None None class 'NoneType' class 'NoneType'
> ## {'day_from': None, 'day_to': None}

1 个答案:

答案 0 :(得分:0)

发生这种情况是因为其中一个字段未清理。您可以通过执行 elseprint(form_date.cleaned_data) 来确认哪个字段,print 应该少一个字段。