[Answered ]-Django form input missing in POST data

2👍

I am not %100 sure why self.cleaned_data.get('repassword') returns None in that method, however clean_password is not the right place for performing validations of fields that depend on each other.

According to docs, you should perform that kind of validation in clean() function:

views.py

def register(self,request):
    form = RegisterForm() # Notice `()` at the end
    return render(request, 'authorization/register.html', {'form': form})

forms.py

...

def clean(self):
    cleaned_data = super(RegisterForm, self).clean()

    password1 = cleaned_data.get('password')
    password2 = cleaned_data.get('repassword')

    if password1 and password1 != password2:
        raise forms.ValidationError("Passwords don't match")

Please note that you don’t have to implement clean_password and clean_repassword.

(If you think you still have to implement clean_password, you have to return password1 from it, not self.cleaned_data.)

You also need to render form errors correctly as described in the docs.

Don’t forget to add it in your template:

{{ form.non_field_errors }}

As for the second error, the problem is, every time the validation fails you are returning a new fresh RegisterForm instance instead of the invalidated one.

You should change the line in create() function:

return render(request, 'authorization/register.html', {'form': RegisterForm}) 

to this:

return render(request, 'authorization/register.html', {'form': form})

Leave a comment