gonzo
gonzo

Reputation: 619

self.clean_data returns None

I have a UserRegistrationForm:

class UserRegisterForm(forms.ModelForm):
    username = forms.CharField(label='enter username', widget=forms.TextInput(attrs={
        'placeholder': 'username', 'class': 'form-control', 'id': 'login'
    }))
    email = forms.CharField(label='enter your email', widget=forms.EmailInput(attrs={
        'placeholder': 'email', 'class': 'form-control', 'id': 'email'
    }))
    password = forms.CharField(label='enter password', validators=[valite_password], widget=forms.PasswordInput(attrs={
        'placeholder': 'password', 'class': 'form-control', 'id': 'password'
    }))
    password2 = forms.CharField(label='confirm password', widget=forms.PasswordInput(attrs={
        'placeholder': 'confirm password', 'class': 'form-control', 'id': 'password2'
    }))

    class Meta:
        model = User
        fields = [
            'username', 'email', 'password', 'password2'
        ]

and it has a clean_password method:

def clean_password(self):
    password = self.cleaned_data.get('password')
    password2 = self.cleaned_data.get('password2')
    print(password)
    print(password2)
    if password != password2:
        raise forms.ValidationError('passwords must match')
    return password

When I send the form data through the POST request, self.cleaned_data.get('password') in my clean_password method will have the value that I entered and self.cleaned_data.get('password2') always returns None, no matter what I entered, and this leads to the exception ValidationError ('passwords must match' ).

What am I doing wrong?

Upvotes: 1

Views: 138

Answers (0)

Related Questions