__init __()得到了关键字参数" input_formats'的多个值。

时间:2015-09-17 21:10:38

标签: python django forms validation datefield

forms.py

from django import forms

ACCEPTED_FORMATS = ['%d-%m-%Y', '%d.%m.%Y', '%d/%m/%Y']

class LeaveRequestForm(forms.Form):
    start_date = forms.DateField(input_formats=ACCEPTED_FORMATS)
    end_date = forms.DateField(input_formats=ACCEPTED_FORMATS)
    working_days = forms.IntegerField(min_value=1)

每次收到“输入有效日期”时,我都找不到可以通过表单验证的日期格式。所以当我尝试定义一些异常时:< / p>

__init__() got multiple values for keyword argument 'input_formats'

https://docs.djangoproject.com/en/1.8/ref/forms/fields/#datefield

settings.py

...

USE_I18N = True

USE_L10N = True

...

我也试过没有运气的内置SelectDateWidget,每次都产生无效日期。也许有一些&#34;缓存&#34;问题: "Enter a valid date" Error in Django Forms DateField

如何通过此验证检查并继续正确的日期,我该怎么做?文档是否遗漏了我遇到的错误?

1 个答案:

答案 0 :(得分:1)

input_formats不是kwarg,它是DateField的唯一选项。放下input_formats=

ACCEPTED_FORMATS = ['%d-%m-%Y', '%d.%m.%Y', '%d/%m/%Y']

class LeaveRequestForm(forms.Form):
    start_date = forms.DateField(ACCEPTED_FORMATS)
    end_date = forms.DateField(ACCEPTED_FORMATS)
    working_days = forms.IntegerField(min_value=1)