简体   繁体   中英

How I can validate fields on forms on identity fields in form. Django

How I can validate fields on forms on identity fields in form instantly without POST request?

my fields in model:

password = models.CharField(max_length=45, verbose_name="")
password2 = models.CharField(max_length=45, verbose_name="")

and my fields in form:

'password': widgets.PasswordInput(attrs={'placeholder': 'New Password'}),
'password2': widgets.PasswordInput(attrs={'placeholder': 'Re-Entere Password'}),

This is the code I tried:

def clean_password(self):
password1 = self.cleaned_data.get('password')
password2 = self.cleaned_data.get('password2')
if not password2:
    raise forms.ValidationError("You must confirm your password")
if password1 != password2:
    raise forms.ValidationError("Your passwords do not match")
return password2 

Code in view:

def saves_data_user_on_registration (request):
    if request.method == 'POST':
        c = {}
        c.update(csrf(request))
        form_user_data = Form_registration(request.POST, request.FILES)
        if form_user_data.is_valid():
            print form_user_data.errors
            form_user_data.save()
            return render_to_response('see_you_later.html', c, context_instance=RequestContext(request))
        else:
            print form_user_data.errors
            return render_to_response('error.html', c, context_instance=RequestContext(request))

I you want to access two fields during validation you need to use clean() not clean_myfield() .

Check the docs.

What do you mean with "without a POST request?".

I would never accept passwords via GET. HTTP GET requests get stored in proxies and caches!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM