Thanks very much!
So now hava two way to solve problems;
one is clean()
def clean(self):
cleaned_data = super(Register,self).clean()
passwd = cleaned_data.get('passwd')
repasswd = cleaned_data.get('repasswd')
if(passwd != repasswd):
self.add_error('repasswd',u'两次密码不一致')
another is clean_passwd():
def clean_passwd(self):
passwd = self.cleaned_data.get('passwd')
repasswd = self.cleaned_data.get('repasswd')
if passwd != repasswd:
raise forms.ValidationError('两次密码不一致')
return passwd
change my code
repasswd = self.cleaned_data['repasswd']
to
repasswd = self.cleaned_data.get('repasswd')
在 2017年7月18日星期二 UTC+8下午5:53:14,Tom Evans写道:
>
> As described in the docs on form and field validation, if you want to
> validate one field using another field, you do so in the clean()
> method:
>
> https://docs.djangoproject.com/en/1.8/ref/forms/validation/
>
> and in detail
>
>
> https://docs.djangoproject.com/en/1.8/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-other
>
>
> Cheers
>
> Tom
>
> On Tue, Jul 18, 2017 at 10:19 AM, 李余通 <[email protected] <javascript:>>
> wrote:
> > My mind is here:
> >
> > class Register(forms.Form):
> > passwd =
> > forms.CharField(max_length=20,label='密码',widget=forms.PasswordInput)
> > repasswd =
> > forms.CharField(max_length=20,label='重复密码',widget=forms.PasswordInput)
> >
> > def clean_passwd(self):
> > passwd = self.cleaned_data['passwd']
> > repasswd = self.cleaned_data['repasswd'] #here is error
> >
> > if passwd != repasswd:
> > raise forms.ValidationError('两次密码不一致')
> > return passwd
> >
> > So,problems is i want to check passwd and repasswd same,How to solve?
> >
> > --
> > You received this message because you are subscribed to the Google
> Groups
> > "Django users" group.
> > To unsubscribe from this group and stop receiving emails from it, send
> an
> > email to [email protected] <javascript:>.
> > To post to this group, send email to [email protected]
> <javascript:>.
> > Visit this group at https://groups.google.com/group/django-users.
> > To view this discussion on the web visit
> >
> https://groups.google.com/d/msgid/django-users/ab8a7050-37c8-4547-9b86-5bd16821ede9%40googlegroups.com.
>
>
> > For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/ad18ff42-3e0a-40a9-a5dd-a531fc9fd7fc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.