我想创建一个表单和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