我想創建一個表單和validation_forms如果要是另一個盒子已覈對無誤在一個盒子一些文本apears會檢查,缺少cleaned_data
class Contact_form(forms.Form):
def __init__(self):
TYPE_CHOICE = (
('C', ('Client')),
('F', ('Facture')),
('V', ('Visite'))
)
self.file_type = forms.ChoiceField(choices = TYPE_CHOICE, widget=forms.RadioSelect)
self.file_name = forms.CharField(max_length=200)
self.file_cols = forms.CharField(max_length=200, widget=forms.Textarea)
self.file_date = forms.DateField()
self.file_sep = forms.CharField(max_length=5, initial=';')
self.file_header = forms.CharField(max_length=200, initial='0')
def __unicode__(self):
return self.name
# Check if file_cols is correctly filled
def clean_cols(self):
#cleaned_data = super(Contact_form, self).clean() # Error apears here
cleaned_file_type = self.cleaned_data.get(file_type)
cleaned_file_cols = self.cleaned_data.get(file_cols)
if cleaned_file_type == 'C':
if 'client' not in cleaned_file_cols:
raise forms.ValidationError("Mandatory fields aren't in collumn descriptor.")
if cleaned_file_type == 'F':
mandatory_field = ('fact', 'caht', 'fact_dat')
for mf in mandatory_field:
if mf not in cleaned_file_cols:
raise forms.ValidationError("Mandatory fields aren't in collumn descriptor.")
def contact(request):
contact_form = Contact_form()
contact_form.clean_cols()
return render_to_response('contact.html', {'contact_form' : contact_form})
Infortunatly,Django的一直說我認爲他沒有重新組合clean_data。我知道我錯過了關於該文檔或某事的一些內容,但是我無法理解這些內容。請幫忙 !
感謝您的建議,但由於某種原因,當我重命名它來清潔它時,我不打電話contact_form.is_valid()? – cp151 2012-07-13 14:20:54
在您看來,您並沒有將表單綁定到任何數據,因此不會調用乾淨的方法。您需要'contact_form = ContactForm(request.POST)'。有關詳細信息,請參閱[文檔](https://docs.djangoproject.com/zh/dev/topics/forms/?from=olddocs#using-a-form-in-a-view)。 – Alasdair 2012-07-13 14:27:40