from django.db import modelsfrom django import formsclass StudentModel(models.Model): id = models.AutoField(primary_key=True) username = models.CharField(max_length=255, null=False, blank=False) password = models.CharField(max_length=255, null=False, blank=False) telephone = models.CharField(max_length=255) address = models.CharField(max_length=255) create_time = models.DateTimeField(auto_created=True) teacher_id = models.ForeignKey('TeacherModel', on_delete=models.CASCADE)class TeacherModel(models.Model): id = models.AutoField(primary_key=True) username = models.CharField(max_length=255, null=False, blank=False) password = models.CharField(max_length=255, null=False, blank=False) telephone = models.CharField(max_length=255) address = models.CharField(max_length=255) create_time = models.DateTimeField(auto_created=True)# 自定义邮箱校验器1def email_validate(value): if "qq" in value: raise forms.ValidationError("不能是qq邮箱")# 自定义邮箱校验器2class EmailValidation: def __call__(self, value): if "sina" in value: raise forms.ValidationError("不能是新浪邮箱")class RegisterForm(forms.Form): username = forms.CharField(label="用户名", required=True, error_messages={ "required": "用户名为空" }) password = forms.CharField(label="密码", required=True, min_length=3, widget=forms.PasswordInput, error_messages={ "required": "密码为空", "min_length": "密码最短为3位" }) password_confirm = forms.CharField(label="确认密码", required=True, min_length=3, widget=forms.PasswordInput, error_messages=({ "required": "密码为空", "min_length": "密码最短为3位" })) email = forms.EmailField(label="邮箱", required=True, validators=[email_validate, EmailValidation()], error_messages=({ 'required': "邮箱为空" })) GENDER_CHOICE = ( ("man", "男"), ("female", "女") ) gender = forms.ChoiceField(label="性别", choices=GENDER_CHOICE, required=True, error_messages=({ "required": "性别为空" })) # 自定义邮箱校验器3 def clean_email(self): if "gmail" in self.cleaned_data.get("email"): raise forms.ValidationError("不能是google邮箱") def clean_password_confirm(self): password = self.cleaned_data.get("password") password_confirm = self.cleaned_data.get("password_confirm") if password and password_confirm and password != password_confirm: raise forms.ValidationError("两次密码不一致") # def clean(self): # password = self.cleaned_data.get("password") # password_confirm = self.cleaned_data.get("password_confirm") # email = self.cleaned_data.get("email") # if password and password_confirm and password != password_confirm: # raise forms.ValidationError("两次密码不一致")