Skip to content Skip to sidebar Skip to footer

Django User Registration AttributeError

When attempting to register a custom built user class, which has a OneToOneField relationship with a django user class, an exception is raised, with no file or line # information.

Solution 1:

you need to return the cleaned_data and not the password1 on the form validation.

your clean should look like the following:

def clean(self):
    # Runs the super methods clean data to validate the form (ensure required fields are filled out and not beyond max lens)
    cleaned_data = super(UserRegistrationForm, self).clean()
    # verify passwords match
    password        = cleaned_data['password']
    password1       = cleaned_data['password1']

    if not password1:
            # raise forms.ValidationError("You must confirm your password!")
            # instead of raising exceptions you should put an error on the form itself.
            self._errors['password1'] = self.error_class(['you must confirm your password'])
    if password != password1:
            # raise forms.ValidationError("Your passwords did not match.")
            # instead of raising exceptions you should put an error on the form itself.
            self._errors['password1'] = self.error_class(['Your passwords did not match'])
    return cleaned_data # return that cleaned data

EDIT

I also want to add, you should absolutely de-couple the form from the models, i know you can tie them in together as you have, but you should separate that logic out into a separate controller.

Your form validations should be pretty lightweight, any additional validations should happen in a separate controller - it makes the code much more re-usable.

also, i can't remember, but i'm positive your additional clean method will never be called. Stick that in the main def clean defition and separate calls out to obtain objects...etc into a User object controller.


Post a Comment for "Django User Registration AttributeError"