Django form.is_valid always returning true
Question: I'm new to Django and python and am trying to learn it from a book. In the
code below I'm trying to make sure password1 and password2 are the same but it
doesn't seem to run the clean_password2 validation because it always returns
true even if they have different values.

    
    
    import re
    from django.contrib.auth.models import User
    from django import forms
    
    class RegistrationForm(forms.Form):
        username = forms.CharField(label=u'Username', max_length=30)
        email = forms.EmailField(label=u'Email')
        password1 = forms.CharField(
            label= u'Password',
            widget = forms.PasswordInput()
        )
        password2 = forms.CharField(
            label= u'Password (Again)',
            widget = forms.PasswordInput()
        )
    
        def clean_password2(self):
            password1 = self.cleaned_data.get('password1')
            password2 = self.cleaned_data.get('password2')
    
            if password1 != password2:
                raise forms.ValidationError('Passwords do not match.')
            return password2
    


Answer: If you are cleaning two fields that depend on each other, you should override
the form's `clean()` method. You can read more
[here](http://docs.djangoproject.com/en/1.2/ref/forms/validation/#cleaning-
and-validating-fields-that-depend-on-each-other).

