How to get extra data in clean_data?

37 views
Skip to first unread message

李余通

unread,
Jul 18, 2017, 5:19:54 AM7/18/17
to Django users
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?

Tom Evans

unread,
Jul 18, 2017, 5:53:14 AM7/18/17
to django...@googlegroups.com
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
> --
> 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 django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> 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.

Jani Tiainen

unread,
Jul 18, 2017, 5:57:35 AM7/18/17
to 'Tom Evans' via Django users
You can see how Django itself validates the password:

https://github.com/django/django/blob/master/django/contrib/auth/forms.py#L64
--
Jani Tiainen

李余通

unread,
Jul 18, 2017, 9:08:41 AM7/18/17
to Django users
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写道:

李余通

unread,
Jul 18, 2017, 9:13:27 AM7/18/17
to Django users
Thank you very much!you are right
I solve the problem:
and I find another way to sovel:
use claea()

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'两次密码不一致')

And last,I want konw how to send a web pag (can auto jumps Specific location) to others,such as you url
"https://github.com/django/django/blob/master/django/contrib/auth/forms.py#L64"
在 2017年7月18日星期二 UTC+8下午5:57:35,Jani Tiainen写道:

Tom Evans

unread,
Jul 18, 2017, 10:48:14 AM7/18/17
to django...@googlegroups.com
On Tue, Jul 18, 2017 at 2:13 PM, 李余通 <liyutong...@gmail.com> wrote:
> Thank you very much!you are right
> I solve the problem:
> [...]
> And last,I want konw how to send a web pag (can auto jumps Specific
> location) to others,such as you url
> "https://github.com/django/django/blob/master/django/contrib/auth/forms.py#L64"

Perhaps reading some simple documentation about HTML might be more
assistance than asking several thousand people to do it for you?

Anyway - Anchors:

http://html.com/anchors-links/

Cheers

Tom
Reply all
Reply to author
Forward
0 new messages