我找到了一个django验证码,但我发现它是3年前编写的,
所以它太旧了吗?
在django中有更好的验证方法
这是代码:
class UserField(forms.CharField):
def clean(self, value):
super(UserField, self).clean(value)
try:
User.objects.get(username=value)
raise forms.ValidationError("Someone is already using this username. Please pick an other.")
except User.DoesNotExist:
return value
class SignupForm(forms.Form):
first_name = forms.CharField(max_length=30)
last_name = forms.CharField(max_length=30)
username = UserField(max_length=30)
password = forms.CharField(widget=forms.PasswordInput())
password2 = forms.CharField(widget=forms.PasswordInput(), label="Repeat your password")
email = forms.EmailField()
email2 = forms.EmailField(label="Repeat your email")
def clean_email(self):
if self.data['email'] != self.data['email2']:
raise forms.ValidationError('Emails are not the same')
return self.data['email']
def clean_password(self):
if self.data['password'] != self.data['password2']:
raise forms.ValidationError('Passwords are not the same')
return self.data['password']
def clean(self,*args, **kwargs):
self.clean_email()
self.clean_password()
return super(SignupForm, self).clean(*args, **kwargs)
感谢
答案 0 :(得分:0)