开发者

is this django validation code too old to use

i find a django validation code ,but i find it was written 3 years ago ,

so is it too old to use?

has some better way to validation in django

this is the code :

class UserField(forms.CharField):
    def clean(self, value):
        super(UserField, self).clean(value)
     开发者_JS百科   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)

thanks


My advice is: don't.

I don't know if this was correct at the time and I'm not willing to test if it still works, but it looks really, really old.

It somewhat resembles the examples in the fine docs, take a look and you will be able to figure out what changed over the last 3 years.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜