nch的注释

pull/41/head
nch 3 months ago
parent f48446ab2a
commit 7004b16736

@ -8,40 +8,73 @@ from . import utils
from .models import BlogUser
# 登录表单继承自Django的AuthenticationForm
class LoginForm(AuthenticationForm):
def __init__(self, *args, **kwargs):
# 调用父类的初始化方法
super(LoginForm, self).__init__(*args, **kwargs)
# 为用户名字段添加占位符和CSS类
self.fields['username'].widget = widgets.TextInput(
attrs={'placeholder': "username", "class": "form-control"})
# 为密码字段添加占位符和CSS类
self.fields['password'].widget = widgets.PasswordInput(
attrs={'placeholder': "password", "class": "form-control"})
# 注册表单继承自Django的UserCreationForm
class RegisterForm(UserCreationForm):
def __init__(self, *args, **kwargs):
# 调用父类的初始化方法
super(RegisterForm, self).__init__(*args, **kwargs)
# 为各个表单字段添加占位符和CSS类
# 为用户名字段自定义widget
self.fields['username'].widget = widgets.TextInput(
attrs={'placeholder': "username", "class": "form-control"})
attrs={
'placeholder': "username", # 输入框内的提示文本,用户点击时会消失
"class": "form-control" # CSS类名用于应用Bootstrap等UI框架的样式
})
# 为邮箱字段自定义widget
self.fields['email'].widget = widgets.EmailInput(
attrs={'placeholder': "email", "class": "form-control"})
attrs={
'placeholder': "email", # 提示用户输入邮箱地址
"class": "form-control" # 统一的表单控件样式类
})
# 为密码字段自定义widget密码输入框1
self.fields['password1'].widget = widgets.PasswordInput(
attrs={'placeholder': "password", "class": "form-control"})
attrs={
'placeholder': "password", # 提示用户输入密码
"class": "form-control" # 密码输入框会显示为圆点或星号,隐藏实际内容
})
# 为确认密码字段自定义widget密码输入框2
self.fields['password2'].widget = widgets.PasswordInput(
attrs={'placeholder': "repeat password", "class": "form-control"})
attrs={
'placeholder': "repeat password", # 提示用户再次输入密码进行确认
"class": "form-control" # 同样使用密码输入框类型
})
# 邮箱验证方法
def clean_email(self):
email = self.cleaned_data['email']
# 检查邮箱是否已存在
if get_user_model().objects.filter(email=email).exists():
raise ValidationError(_("email already exists"))
return email
# 元类配置
class Meta:
# 指定使用的用户模型
model = get_user_model()
# 表单包含的字段
fields = ("username", "email")
# 忘记密码表单
class ForgetPasswordForm(forms.Form):
# 新密码字段
new_password1 = forms.CharField(
label=_("New password"),
widget=forms.PasswordInput(
@ -52,6 +85,7 @@ class ForgetPasswordForm(forms.Form):
),
)
# 确认密码字段
new_password2 = forms.CharField(
label="确认密码",
widget=forms.PasswordInput(
@ -62,6 +96,7 @@ class ForgetPasswordForm(forms.Form):
),
)
# 邮箱字段
email = forms.EmailField(
label='邮箱',
widget=forms.TextInput(
@ -72,6 +107,7 @@ class ForgetPasswordForm(forms.Form):
),
)
# 验证码字段
code = forms.CharField(
label=_('Code'),
widget=forms.TextInput(
@ -82,17 +118,21 @@ class ForgetPasswordForm(forms.Form):
),
)
# 确认密码验证方法
def clean_new_password2(self):
password1 = self.data.get("new_password1")
password2 = self.data.get("new_password2")
# 检查两次输入的密码是否一致
if password1 and password2 and password1 != password2:
raise ValidationError(_("passwords do not match"))
# 验证密码强度
password_validation.validate_password(password2)
return password2
# 邮箱验证方法
def clean_email(self):
user_email = self.cleaned_data.get("email")
# 检查邮箱是否存在
if not BlogUser.objects.filter(
email=user_email
).exists():
@ -100,8 +140,10 @@ class ForgetPasswordForm(forms.Form):
raise ValidationError(_("email does not exist"))
return user_email
# 验证码验证方法
def clean_code(self):
code = self.cleaned_data.get("code")
# 验证邮箱和验证码是否匹配
error = utils.verify(
email=self.cleaned_data.get("email"),
code=code,
@ -111,7 +153,9 @@ class ForgetPasswordForm(forms.Form):
return code
# 忘记密码验证码请求表单
class ForgetPasswordCodeForm(forms.Form):
# 邮箱字段,用于发送验证码
email = forms.EmailField(
label=_('Email'),
)

Loading…
Cancel
Save