Python 电子邮件验证检查

Python 电子邮件验证检查,python,django,django-forms,django-views,django-templates,Python,Django,Django Forms,Django Views,Django Templates,我希望在“我的电子邮件表单”字段中进行电子邮件检查,以便如果用户的电子邮件没有以“@aun.edu.ng”结尾,它将通过错误消息进行检查 forms.py class EmployeeRegistrationForm(forms.ModelForm): first_name = forms.CharField(label='First Name') last_name = forms.CharField(label='Last Name') cv = forms.File

我希望在“我的电子邮件表单”字段中进行电子邮件检查,以便如果用户的电子邮件没有以“@aun.edu.ng”结尾,它将通过错误消息进行检查

forms.py

class EmployeeRegistrationForm(forms.ModelForm):
    first_name = forms.CharField(label='First Name')
    last_name = forms.CharField(label='Last Name')
    cv = forms.FileField(label='CV')
    skills_or_trainings = forms.SlugField(label='Skills or Trainings', required=False, widget=forms.Textarea)
    class Meta:
        model = Student
        fields = ['first_name', 'last_name', 'gender', 'level', 'school', 'major', 'cv', 'skills_or_trainings' ]



class EmployerRegistrationForm(forms.ModelForm):
    company_name = forms.CharField(label="Company Name")
    company_address = forms.CharField(label="Company Address")
    website = forms.CharField()

    class Meta:
        model = Employer
        fields = ['company_name', 'company_address', 'website']
views.py

def student_register(request):
    if request.method == 'POST':
        form = UserRegistrationForm(request.POST)
        student_form = EmployeeRegistrationForm(request.POST)

        if form.is_valid() and student_form.is_valid():
            user = form.save(commit=False)
            user.is_student = True
            user.save()

            student = student_form.save(commit=False)
            student.user = user
            student.save()
            return redirect('login')
    else:
        form = UserRegistrationForm()
        student_form = EmployeeRegistrationForm()
    
    context = {'form': form, 'student_form': student_form}
    return render (request, 'accounts/employee/register.html', context)

def employer_register(request):
    if request.method == 'POST':
        form = UserRegistrationForm(request.POST)
        employer_form = EmployerRegistrationForm(request.POST)

        if form.is_valid() and employer_form.is_valid():
            user = form.save(commit=False)
            user.is_employer = True
            user.save()

            employer = employer_form.save(commit=False)
            employer.user = user
            employer.save()
            return redirect('login')
    else:
        form = UserRegistrationForm()
        employer_form = EmployerRegistrationForm()
    
    context = {'form': form, 'employer_form': employer_form}
    return render (request, 'accounts/employer/register.html', context)
请告诉我如何处理,以便当用户发送的电子邮件不是以“@aun.edu.ng”结尾时,我能够通过验证错误消息 感谢您

请参阅与电子邮件地址匹配的正则表达式的答案。您可以在Python中将其与模块一起使用。如果电子邮件无效,只需将其发送回登录名,并显示一条消息,说明电子邮件地址无效

重新导入
(以下简称::[a-z0-10-10-10-10-10-10-10-5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5-10 10-5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 0 0 0 0 0 0 0 0-10 10 10 10 10 10-10 10 10 10 10 10除了除了除了除了除了除了除了除了除了除了除了以下以下以下以下几种主要的主要的主要的现场现场现场现场现场进行进行进行进行进行进行现场现场现场现场现场比赛,再再若若若若若-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10 10-10-10-10 10-10-10-10 10 10 10 10 10-10 10-10 10 10-10 10-10 10 10 10-10 10-10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10[a-z0-9-]*[a-z0-9])?\)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\[(?:(?:(?:)(2(5[0-5]|[0-4][0-9])| 1[0-9][0-9]|[1-9]|[0-9])){3]((2(5[0-5]1240-4][0-9])1[0-9])?[0-9]。[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]。\[\x01-\x09\x0b\x0c\x0e-\x7f])+)”
如果重新匹配(发送电子邮件)someone@example.com"):
打印(“找到匹配项”)
其他:
打印(“无效电子邮件地址”)

请注意,有效的电子邮件地址包括未注册的电子邮件地址,完整验证电子邮件地址的唯一方法是向他们发送一封带有验证链接的电子邮件。该链接将允许他们“解锁”创建的帐户。

要向雇主发出验证错误,您可以通过覆盖
clean()
员工登记表的方法

来自django.form的导入验证错误 类EmployerRegistrationForm(forms.ModelForm): 公司名称=forms.CharField(label=“公司名称”) 公司地址=forms.CharField(label=“公司地址”) website=forms.CharField() 类元: 模型=雇主 字段=['公司名称','公司地址','网站'] def清洁(自清洁): cleaned_data=super().clean() 如果清除了数据获取('company_address')。endswith('aun.edu.ng'): raise ValidationError('此电子邮件不适用') 返回已清除的数据
因此,我会将允许的字母放入电子邮件中?我如何发送电子邮件验证链接我希望电子邮件验证仅针对学生模型,然后可能可以将验证链接发送到所有模型。我如何操作?除了regex方法外,还有其他方法吗?``导入电子邮件验证链接本次研究的目的是:“(?:[a-z0-10-10-10-10-10-10-10现场现场现场现场教学人员:””(以下以下简称:“(以下以下以下简称::”(以下以下简称:::[a-z0-10-10-10-10-10-10-10-10-10-10-10-9-10-5 5-8.[除除除以下以下简称::::::::::”)除除除除以下以下以下简称::“((以下以下以下以下以下以下简称:::::::::::::::::”)除除除除除除除以下以下以下以下以下以下以下以下以下以下简称:(a-a-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-10-[a-z0-9-]*[a-z0-9]?\)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?[(?:(?:(?:(2(5[0-5]|[0-4][0-9])| 1[0-9][0-9]|[1-9]|[0-9])){3:((2(5[0-5]1240-4][0-9])1[0-1249][0-9]。[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]。[\x01-\x09\x0b\x0c\x0e-\x7f])+)“如果重新匹配(电子邮件,”someone@aun.edu.ng“”:打印(“找到匹配项”)其他:打印(“无效电子邮件地址”)```那么,这就是我将在我的views.py或forms.py中编写的内容。我是否可以仅针对将在学生电子邮件下注册的电子邮件提出验证错误?是的,您可以,除了您在
EmployeeRegistrationForm
中提供的“没有电子邮件”字段之外。电子邮件字段是所有模型的通用字段,因此我将其放在t中用户模型。那么,是否有可能将验证仅限于学生?