这个django验证码太旧了,无法使用

时间:2010-12-10 02:32:43

标签: python django validation

我找到了一个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)

感谢

1 个答案:

答案 0 :(得分:0)

我的建议是:不要。

我当时不知道这是否正确,我不愿意测试它是否仍然有效,但看起来真的很老了。

它有点类似于fine docs中的示例,看一看,您将能够找出过去3年中发生了哪些变化。