我想驗證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)
什麼日期,你想進入? – 2012-02-23 09:29:28
向我們顯示輸入。向我們顯示您擁有的任何非字段表單代碼。向我們展示相關的視圖代碼。 – Marcin 2012-02-23 09:40:21
什麼是settings.TIME_ZONE? – 2012-02-23 10:33:02