1. from django.db import models
    2. from django import forms
    3. class StudentModel(models.Model):
    4. id = models.AutoField(primary_key=True)
    5. username = models.CharField(max_length=255, null=False, blank=False)
    6. password = models.CharField(max_length=255, null=False, blank=False)
    7. telephone = models.CharField(max_length=255)
    8. address = models.CharField(max_length=255)
    9. create_time = models.DateTimeField(auto_created=True)
    10. teacher_id = models.ForeignKey('TeacherModel', on_delete=models.CASCADE)
    11. class TeacherModel(models.Model):
    12. id = models.AutoField(primary_key=True)
    13. username = models.CharField(max_length=255, null=False, blank=False)
    14. password = models.CharField(max_length=255, null=False, blank=False)
    15. telephone = models.CharField(max_length=255)
    16. address = models.CharField(max_length=255)
    17. create_time = models.DateTimeField(auto_created=True)
    18. # 自定义邮箱校验器1
    19. def email_validate(value):
    20. if "qq" in value:
    21. raise forms.ValidationError("不能是qq邮箱")
    22. # 自定义邮箱校验器2
    23. class EmailValidation:
    24. def __call__(self, value):
    25. if "sina" in value:
    26. raise forms.ValidationError("不能是新浪邮箱")
    27. class RegisterForm(forms.Form):
    28. username = forms.CharField(label="用户名", required=True, error_messages={
    29. "required": "用户名为空"
    30. })
    31. password = forms.CharField(label="密码", required=True, min_length=3, widget=forms.PasswordInput, error_messages={
    32. "required": "密码为空",
    33. "min_length": "密码最短为3位"
    34. })
    35. password_confirm = forms.CharField(label="确认密码", required=True, min_length=3, widget=forms.PasswordInput,
    36. error_messages=({
    37. "required": "密码为空",
    38. "min_length": "密码最短为3位"
    39. }))
    40. email = forms.EmailField(label="邮箱", required=True, validators=[email_validate, EmailValidation()], error_messages=({
    41. 'required': "邮箱为空"
    42. }))
    43. GENDER_CHOICE = (
    44. ("man", "男"),
    45. ("female", "女")
    46. )
    47. gender = forms.ChoiceField(label="性别", choices=GENDER_CHOICE, required=True, error_messages=({
    48. "required": "性别为空"
    49. }))
    50. # 自定义邮箱校验器3
    51. def clean_email(self):
    52. if "gmail" in self.cleaned_data.get("email"):
    53. raise forms.ValidationError("不能是google邮箱")
    54. def clean_password_confirm(self):
    55. password = self.cleaned_data.get("password")
    56. password_confirm = self.cleaned_data.get("password_confirm")
    57. if password and password_confirm and password != password_confirm:
    58. raise forms.ValidationError("两次密码不一致")
    59. # def clean(self):
    60. # password = self.cleaned_data.get("password")
    61. # password_confirm = self.cleaned_data.get("password_confirm")
    62. # email = self.cleaned_data.get("email")
    63. # if password and password_confirm and password != password_confirm:
    64. # raise forms.ValidationError("两次密码不一致")