django表单dateField验证失败

时间:2012-02-23 09:17:52

标签: django django-forms datefield

我正在尝试验证django中的用户配置文件表单,但我不能。 form.dateField()似乎有问题。它不验证(即.is_valid()返回false)

这是我的表单dateField条目: date_of_birth = forms.DateField(label=u'date of birth', input_formats='%d/%m/%Y', required=False, widget=forms.DateInput(format = '%d/%m/%Y'))

我注意到request.POST.get('date_of_birth', '')返回正确的日期(即我在html表单字段中输入的日期)。

我也注意到在这个函数中:

def clean_date_of_birth(self):
    date = self.cleaned_data['date_of_birth']

日期对象始终为无。

我做错了什么?

编辑:

这是我想要输入的内容: 29/07/1974(1974年7月29日)

这是'submit'(各种请求)的输出

29/07/1974
profile form is *NOT* valid
[23/Feb/2012 12:16:27] "POST /profile/chris/ HTTP/1.1" 200 16289

29/7/1974
profile form is *NOT* valid
[23/Feb/2012 12:16:33] "POST /profile/chris/ HTTP/1.1" 200 16289

1974-07-29
profile form is *NOT* valid
[23/Feb/2012 12:18:15] "POST /profile/chris/ HTTP/1.1" 200 16289

这是我的模板

    <div class="input_area">
        <form id="profile_form" method="post" action="/profile/{{ user.username }}/">{% csrf_token %}
            {{ form.as_p }}
            <input type="submit" id="submit" value="save" class="submitButton idle" style="width:70px" />
        </form>
    </div>

这是我的views.py

def profile(request, username):
    form = ProfileForm(request.POST)
    print request.POST.get('date_of_birth', 'None')
    try:
        user = User.objects.get(username=username)
    except User.DoesNotExist:
        raise Http404(u'User not Found')
    if form.is_valid():
        print 'profile form is valid'
    else:
        print 'profile form is *NOT* valid'

最后这是我的forms.py(目前不要使用clean_data函数)

class ProfileForm(forms.Form):

    tz = []
    timezones = Timezone.objects.all()
    for timezone in timezones:
        val = str(timezone.hour)
        v = val.split(':')
        tuple = (timezone.id, '('+timezone.sign+''+v[0]+':'+v[1]+') '+timezone.name)
        tz.append(tuple)

    sex = [('male','male'),('female', 'female'),('unknown', 'prefer not to say')]
    real_name = forms.CharField(label=u'real name', widget=forms.TextInput, required=False)
    date_of_birth = forms.DateField(label=u'date of birth', input_formats='%d/%m/%Y', required=False, widget=forms.DateInput(format = '%d/%m/%Y'))
    pp_email = forms.EmailField(label=u'Paypal Email', widget=forms.TextInput, required=False)
    gender = forms.ChoiceField(label=u'sex', choices=sex, widget=forms.Select(), required=False)
    timezone = forms.ChoiceField(label=u'time zone', choices=tz, widget=forms.Select())
    address = forms.CharField(label=u'street address', widget=forms.Textarea, required=False)
    postal  = forms.CharField(label=u'postal code', widget=forms.TextInput, required=False)

2 个答案:

答案 0 :(得分:6)

答案 1 :(得分:1)

使用Django 1.6及更高版本,您可以在表单中使用表单中的localized_fieldslocalize=True。请参阅https://docs.djangoproject.com/en/1.9/topics/i18n/formatting/#format-localization

使用USE_L10N = True时,Django会将formats.py文件用于您的语言区域(LANGUAGE_CODE的一部分)。

你最终会得到像这样的干事(因为fields中指定的models.py不需要在forms.py中重复):

class SomeForm(forms.Form):

    class Meta:
        model = SomeModel
        fields = ('first_name', 'dob',)
        localized_fields = ('dob',)