2012-02-23 144 views
4

我想验证Django中的用户分析表单,我不能。这似乎是forms.dateField()有问题。它不验证django表单dateField失败验证

这是我的形式的DateField条目(即is_valid()返回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'))

我注意到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日)

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

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> 

这是我的看法s.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) 
+0

什么日期,你想进入? – 2012-02-23 09:29:28

+0

向我们显示输入。向我们显示您拥有的任何非字段表单代码。向我们展示相关的视图代码。 – Marcin 2012-02-23 09:40:21

+0

什么是settings.TIME_ZONE? – 2012-02-23 10:33:02

回答

6
+0

等等,这是如何解决问题的?我有同样的问题,我没有指定自定义输入格式,这应该回落到默认值。该日期仍然无法验证。 – 2012-03-11 01:43:54

+1

它为我做了。 'date_of_birth = forms.DateField(label = u'date of birth',input_formats = ['%d /%m /%Y','%m /%d /%Y',],required = False,widget = forms .DateInput(format ='%d /%m /%Y'))' – xpanta 2012-03-16 06:33:11

1

随着Django 1.6及更高版本,您可以在表单的Meta或localize=True中使用localized_fields。见https://docs.djangoproject.com/en/1.9/topics/i18n/formatting/#format-localization

当使用USE_L10N = True时,Django将使用formats.py文件作为您的语言环境(LANGUAGE_CODE的一部分)。

你可以用有事干这样结束了(在models.py指定的fields不需要forms.py要重复):

class SomeForm(forms.Form): 

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