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)
每次收到"输入有效日期"时,我都找不到任何可以通过表单验证的日期格式,所以当我试图定义一些时,出现了一个例外:
__init__() got multiple values for keyword argument 'input_formats'
https://docs.djangoproject.com/en/1.8/ref/forms/fields/#datefield
设置.py
USE_I18N = True
USE_L10N = True
我还尝试了内置的SelectDateWidget,但没有成功,每次都产生无效的日期。也许存在一些"缓存"问题:"输入有效日期";Django Forms DateField 中的错误
我应该怎么做才能通过验证检查并确定合适的日期?我遇到错误,文档是否缺少什么?
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)