python – 表单中缺少cleaning_data(django)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 表单中缺少cleaning_data(django),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2170字,纯文字阅读大概需要4分钟。
内容图文
![python – 表单中缺少cleaning_data(django)](/upload/InfoBanner/zyjiaocheng/770/dfe06485e6744d65a61a968a5cee8a4d.jpg)
我想创建一个表单和validation_forms,如果正确检查了另一个框,将检查某个文本是否在框中出现,
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})
不幸的是,django一直在告诉我他没有重新认识clean_data.我知道我已经错过了关于doc或者某些东西的东西,但我无法明白什么.请帮忙 !
解决方法:
验证单个字段时,clean方法应具有表单的名称
clean_<name of field>
例如clean_file_col.然后,当您在视图中执行form.is_valid()时,它将自动调用.
命名方法clean_cols表明你有一个名为cols的字段,这可能会引起混淆.
在这种情况下,你的validation relies on other fields,所以你应该重命名clean_col方法简单地清理.这样,当您在视图中执行form.is_valid()时,它将自动调用.
def clean(self):
cleaned_data = super(Contact_form, self).clean()
cleaned_file_type = self.cleaned_data.get(file_type)
# ...
最后,在您看来,您还没有将表单绑定到任何数据,
contact_form = Contact_form()
所以contact_form.is_valid()将始终返回False.您需要使用form = ContactForm(request.POST)将表单绑定到post数据.有关完整示例和说明,请参阅Django docs for using a form in a view.
内容总结
以上是互联网集市为您收集整理的python – 表单中缺少cleaning_data(django)全部内容,希望文章能够帮你解决python – 表单中缺少cleaning_data(django)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。