From df8aa25c3678989163301b251bcc7b263e80e207 Mon Sep 17 00:00:00 2001 From: peps7ac8w <249306157@qq.com> Date: Sun, 9 Nov 2025 21:42:23 +0800 Subject: [PATCH] ADD file via upload --- forms.py | 136 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 forms.py diff --git a/forms.py b/forms.py new file mode 100644 index 0000000..1d67149 --- /dev/null +++ b/forms.py @@ -0,0 +1,136 @@ +from django import forms +from django.contrib.auth import get_user_model, password_validation +from django.contrib.auth.forms import AuthenticationForm, UserCreationForm +from django.core.exceptions import ValidationError +from django.forms import widgets +from django.utils.translation import gettext_lazy as _ +from . import utils +from .models import BlogUser + + +class LoginForm(AuthenticationForm): + #lht: 登录表单,继承Django内置认证表单 + def __init__(self, *args, **kwargs): + #lht: 调用父类构造函数 + super(LoginForm, self).__init__(*args, **kwargs) + #lht: 自定义用户名和密码字段的显示样式 + self.fields['username'].widget = widgets.TextInput( + attrs={'placeholder': "username", "class": "form-control"}) + self.fields['password'].widget = widgets.PasswordInput( + attrs={'placeholder': "password", "class": "form-control"}) + + +class RegisterForm(UserCreationForm): + #lht: 用户注册表单,继承Django内置用户创建表单 + def __init__(self, *args, **kwargs): + #lht: 调用父类构造函数 + super(RegisterForm, self).__init__(*args, **kwargs) + #lht: 自定义各字段的显示样式 + self.fields['username'].widget = widgets.TextInput( + attrs={'placeholder': "username", "class": "form-control"}) + self.fields['email'].widget = widgets.EmailInput( + attrs={'placeholder': "email", "class": "form-control"}) + self.fields['password1'].widget = widgets.PasswordInput( + attrs={'placeholder': "password", "class": "form-control"}) + self.fields['password2'].widget = widgets.PasswordInput( + attrs={'placeholder': "repeat password", "class": "form-control"}) + + def clean_email(self): + #lht: 验证邮箱唯一性 + email = self.cleaned_data['email'] + if get_user_model().objects.filter(email=email).exists(): + raise ValidationError(_("email already exists")) + return email + + class Meta: + #lht: 指定模型和字段 + model = get_user_model() + fields = ("username", "email") + + +class ForgetPasswordForm(forms.Form): + #lht: 忘记密码表单 + #lht: 新密码字段 + new_password1 = forms.CharField( + label=_("New password"), + widget=forms.PasswordInput( + attrs={ + "class": "form-control", + 'placeholder': _("New password") + } + ), + ) + + #lht: 确认新密码字段 + new_password2 = forms.CharField( + label="确认密码", + widget=forms.PasswordInput( + attrs={ + "class": "form-control", + 'placeholder': _("Confirm password") + } + ), + ) + + #lht: 邮箱字段 + email = forms.EmailField( + label='邮箱', + widget=forms.TextInput( + attrs={ + 'class': 'form-control', + 'placeholder': _("Email") + } + ), + ) + + #lht: 验证码字段 + code = forms.CharField( + label=_('Code'), + widget=forms.TextInput( + attrs={ + 'class': 'form-control', + 'placeholder': _("Code") + } + ), + ) + + def clean_new_password2(self): + #lht: 验证两次输入的密码是否一致 + 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")) + #lht: 验证密码强度 + password_validation.validate_password(password2) + + return password2 + + def clean_email(self): + #lht: 验证邮箱是否存在 + user_email = self.cleaned_data.get("email") + if not BlogUser.objects.filter( + email=user_email + ).exists(): + #lht: todo 这里的报错提示可以判断一个邮箱是不是注册过,如果不想暴露可以修改 + raise ValidationError(_("email does not exist")) + return user_email + + def clean_code(self): + #lht: 验证验证码是否正确 + code = self.cleaned_data.get("code") + #lht: 调用工具函数验证验证码 + error = utils.verify( + email=self.cleaned_data.get("email"), + code=code, + ) + if error: + raise ValidationError(error) + return code + + +class ForgetPasswordCodeForm(forms.Form): + #lht: 忘记密码时获取验证码的表单 + #lht: 邮箱字段 + email = forms.EmailField( + label=_('Email'), + )