Spaces:
Sleeping
Sleeping
| from django import forms | |
| from django.contrib.auth.models import User | |
| from django.contrib.auth.forms import AuthenticationForm | |
| class RegisterForm(forms.ModelForm): | |
| password = forms.CharField(widget=forms.PasswordInput, min_length=8) | |
| confirm_password = forms.CharField(widget=forms.PasswordInput) | |
| class Meta: | |
| model = User | |
| fields = ["username", "email", "password"] | |
| def clean_username(self): | |
| """验证用户名是否已存在""" | |
| username = self.cleaned_data.get('username') | |
| if username and User.objects.filter(username=username).exists(): | |
| raise forms.ValidationError("该用户名已被注册,请使用其他用户名") | |
| return username | |
| def clean_email(self): | |
| """验证邮箱是否已存在""" | |
| email = self.cleaned_data.get('email') | |
| if email and User.objects.filter(email=email).exists(): | |
| raise forms.ValidationError("该邮箱已被注册,请使用其他邮箱") | |
| return email | |
| def clean(self): | |
| cleaned = super().clean() | |
| password = cleaned.get("password") | |
| confirm_password = cleaned.get("confirm_password") | |
| if password and confirm_password and password != confirm_password: | |
| raise forms.ValidationError("两次密码不一致") | |
| return cleaned | |
| class LoginForm(AuthenticationForm): | |
| username = forms.CharField(max_length=150) | |
| password = forms.CharField(widget=forms.PasswordInput) |