tags:

views:

54

answers:

1

Hi

can any body suggest me any idea about how can i compare two fields in django. as i have two password fields in my forms.py file. now i want to compare the two fields and if both are same then save the user in database else append an error message to reenter the values again.

thanks

+1  A: 

Override your form's clean method:

class MyRegistrationForm(forms.Form):
    password1=...
    password2=...
    ...

def clean(self):
    cleaned_data = self.cleaned_data # individual field's clean methods have already been called
    password1 = cleaned_data.get("password1")
    password2 = cleaned_data.get("password2")
    if password1 != password2:
        raise forms.ValidationError("Passwords must be identical.")

    return cleaned_data

See the docs for more info.

You should also probably ALSO add some Javascript to check this on the client side - client side validation is no subsitute for server-side validation, but it is more responsive to the user and saves bandwidth.

Chris Lawlor