在Django后端仔细检查电子邮件验证

时间:2019-06-16 04:55:53

标签: python django validation

考虑到用户可以在浏览器中禁用JavaScript,我该如何在 Django 后端进行“双重检查”以进行电子邮件验证(具体来说,是为了防止使用Webmail [GMail注册] ,Hotmail等])?谢谢。

1 个答案:

答案 0 :(得分:0)

您可以在您的forms.py(必须创建该文件)应用中执行此操作

from django import forms

class ContactForm(forms.Form):
    full_name = forms.CharField()
    email = forms.EmailField()

    def clean_email(self, *args, **kwargs):
        email = self.cleaned_data.get('email')
        if email.endswith('@gmail.com'):  # etc.
            raise forms.ValidationError('This is not a valid email')
        return email

Here you can find more about it