我的新闻稿表单中有一个电子邮件字段,如下所示:
class NewsletterForm(forms.ModelForm):
email = forms.EmailField(widget=forms.EmailInput(attrs={
'autocomplete': 'off',
'class': 'form-control',
'placeholder': _('seuemail@email.com'),
'required': 'required'
}))
class Meta:
model = Newsletter
fields = ['email', ]
我的表格正在运作,但是当我输入" ahasudah @ ahs"没有域名的DOT,我收到此错误"输入有效的电子邮件地址"
这是哪里?
我刚检查了原始来源,但我找不到像其他字段一样覆盖的错误消息。
https://github.com/django/django/blob/master/django/forms/fields.py#L523
有关如何覆盖此消息的任何想法?
答案 0 :(得分:4)
事实上,您可以在两个不同的级别以两种不同的方式执行此操作:
class NewsletterForm(forms.ModelForm):
email = forms.EmailField(
widget=forms.EmailInput(attrs={
'autocomplete': 'off',
'class': 'form-control',
'placeholder': _('seuemail@email.com'),
'required': 'required'
}),
error_messages={'invalid': 'your custom error message'}
)
class Meta:
model = Newsletter
fields = ['email', ]
2.1。你可以像以下形式那样做:
email = models.EmailField(error_messages={'invalid':"you custom error message"})
2.2。或者你使用django built-in Validators:
from django.core.validators import EmailValidator
email = models.EmailField(validators=[EmailValidator(message="your custom message")]) # in you model class