我正在尝试将符合Django文档的验证器添加到我的字段中,但是即使字段中没有“ fred@example.com”,每次我都能发送电子邮件时,我都无法获得效果
我是否必须在视图中添加任何其他代码行?任何帮助将不胜感激。
class EmailContactForm(forms.Form):
e_mail = forms.EmailField()
tresc_wiadomosci = forms.CharField(widget=forms.Textarea)
def clean_recipients(self):
data = self.cleaned_data['e_mail']
if "fred@example.com" not in data:
raise forms.ValidationError("You have forgotten about Fred!")
答案 0 :(得分:2)
将clean_recipients
更改为clean_e_mail
class EmailContactForm(forms.Form):
e_mail = forms.EmailField()
tresc_wiadomosci = forms.CharField(widget=forms.Textarea)
def clean_e_mail(self):
data = self.cleaned_data['e_mail']
if "fred@example.com" not in data:
raise forms.ValidationError("You have forgotten about Fred!")
答案 1 :(得分:0)
如果要覆盖表单的clean
方法,则需要为其命名与要清理的字段相同的名称。在这种情况下,您应该将方法命名为clean_e_mail
。