django表单dateField验证失败 [英] django forms dateField fails validation

查看:113
本文介绍了django表单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自我):
date = self.cleaned_data ['date_of_birth']

date对象总是没有。



我做错了什么?



编辑:



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



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

  29/07/1974 
个人资料表单是* NOT * valid
[23 / Feb / 2012 12:16: 27]POST / profile / chris / HTTP / 1.1200 16289

29/7/1974
个人资料表单是* NOT * valid
[23 / Feb / 2012 12 :16:33]POST / profile / chris / HTTP / 1.1200 16289

1974-07-29
个人资料表单是* NOT * valid
[23 / Feb / 2012 12:18:15]POST / profile / chris / HTTP / 1.1200 16289

这是我的模板。

 < div class =input_area> 
< form id =profile_formmethod =postaction =/ profile / {{user.username}} /> {%csrf_token%}
{{form.as_p}}
< input type =submitid =submitvalue =saveclass =submitButton idlestyle =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)
除了User.DoesNotExist:
raise Http404(u'User not Found')
如果表单。 is_valid():
print'个人资料表单有效'
else:
print'个人资料表单是* NOT * valid'

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

  class ProfileForm(forms.Form):

tz = []
timezones = Timezone.objects.all()
时区中的时区:
val = str(timezone.hour)
v = val.split(':')
tuple =(timezone.id,'('+ timezone.sign +''+ v [0] +' :'+ v [1] +')'+ timezone.name)
tz.append(tup le)

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 = form.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)


<必须在DateField中的div class =h2_lin>解决方案

输入格式列表或元组 https://docs.djangoproject.com/en/ dev / ref / forms / fields /#django.forms.DateField.input_formats


I am trying to validate a User Profiling form in django and I can't. It seems that there is something wrong with forms.dateField(). It does not validate (ie. is_valid() return false)

this is my forms dateField entry: date_of_birth = forms.DateField(label=u'date of birth', input_formats='%d/%m/%Y', required=False, widget=forms.DateInput(format = '%d/%m/%Y'))

I noticed that request.POST.get('date_of_birth', '') returns the correct date (ie. the date I have typed in the html form field).

I also noticed that in this function:

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

date object is always None.

What am I doing wrong?

EDIT:

This is what I am trying to enter: 29/07/1974 (July 29th, 1974)

This is the output of 'submit' (various requests)

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

This is my template

    <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>

this is my 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'

and finally this is my forms.py (do not use clean_data functions at the moment)

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)

解决方案

input formats in DateField must be list or tuple https://docs.djangoproject.com/en/dev/ref/forms/fields/#django.forms.DateField.input_formats

这篇关于django表单dateField验证失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆