我有一個表格,其失敗is_valid()
測試。我創建了一個通過相同數據輸入的測試的相同表單。不知道爲什麼會通過,一個會失敗。無效的Django表格
這裏是形式:
choices = ((1,'Yes'),(0,'No'),
)
class ActivitySaveForm(forms.Form):
name = forms.CharField(
label=u'Activity Name',
widget=forms.TextInput(attrs={'size': 64})
)
url = forms.URLField(
label=u'URL',
widget=forms.TextInput(attrs={'size': 64})
)
desc = forms.CharField(
label=u'Describe it',
widget=forms.TextInput(attrs={'size': 250})
)
created = forms.DateField(
label=u'Date Entered',initial=datetime.date.today,
)
priority = forms.CharField(
label=u'priority: 1-5',
widget=forms.TextInput(attrs={'size': 1})
)
difficulty = forms.CharField(
label=u'How hard is it? 1-5',
widget=forms.TextInput(attrs={'size': 1})
)
done = forms.TypedChoiceField(choices=choices, widget=forms.RadioSelect, coerce=int
)
tags = forms.CharField(
label=u'Tags',required=False,
widget=forms.TextInput(attrs={'size': 64})
)
#def __init__(self, *args, **kwargs):
#super(CircuitForm, self).__init__(*args, **kwargs)
#for key in self.fields:
#self.fields[key].required = False
class AcTest(forms.Form):
name = forms.CharField(
label=u'Activity Name',
widget=forms.TextInput(attrs={'size': 64})
)
url = forms.URLField(
label=u'URL',
widget=forms.TextInput(attrs={'size': 64})
)
desc = forms.CharField(
label=u'Describe it',
widget=forms.TextInput(attrs={'size': 250})
)
created = forms.DateField(
label=u'Date Entered',initial=datetime.date.today,
)
priority = forms.CharField(
label=u'priority: 1-5',
widget=forms.TextInput(attrs={'size': 1})
)
difficulty = forms.CharField(
label=u'How hard is it? 1-5',
widget=forms.TextInput(attrs={'size': 1})
)
done = forms.TypedChoiceField(choices=choices, widget=forms.RadioSelect, coerce=int
)
tags = forms.CharField(
label=u'Tags',required=False,
widget=forms.TextInput(attrs={'size': 64})
)
下面是測試,結果兩種。
>>> data = {'name':'test',
... 'url': 'www.test.com',
... 'desc':'test desc',
... 'created': '01/01/1900',
... 'priority':1,
... 'difficulty':1,
... 'desc':'test desc',
... 'tags':'test desc'}
>>> f=ActivitySaveForm(data)
>>> f.is_valid()
False
>>> f=AcTest(data)
>>> f.is_valid()
True
不知道如何進一步診斷此錯誤,並沒有找到一種方法使其工作。感謝您的幫助。
謝謝jbrendel。尷尬。這是問題。 – jabs 2012-03-02 23:01:30