ubernostrum / django-registration

An extensible user-registration app for Django.
BSD 3-Clause "New" or "Revised" License
923 stars 241 forks source link

AttributeError: 'unicode' object has no attribute 'get' #165

Closed HenryMehta closed 6 years ago

HenryMehta commented 6 years ago

I'm trying to set the username to the email field and have created the following form:

attrs_dict = {'class': 'required'}

class NoUsernameRegistrationForm(RegistrationForm):
    """
    Form for registering a new user account.

    Requires the password to be entered twice to catch typos.

    Subclasses should feel free to add any additional validation they
    need, but should avoid defining a ``save()`` method -- the actual
    saving of collected user data is delegated to the active
    registration backend.

    """
    username = forms.CharField(
        widget=forms.EmailInput(attrs=dict(attrs_dict, maxlength=75)),
        label=_("Email address"))
    password1 = forms.CharField(
        widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
        label=_("Password"))
    password2 = forms.CharField(
        widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
        label=_("Password (again)"))
    email = forms.EmailField(
        widget=forms.HiddenInput(),
        required = False)

    def clean(self):
        """
        Verify that the values entered into the two password fields
        match. Note that an error here will end up in
        ``non_field_errors()`` because it doesn't apply to a single
        field.

        """
        if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
            if self.cleaned_data['password1'] != self.cleaned_data['password2']:
                raise forms.ValidationError(_("The two password fields didn't match."))

        """
        Validate that the email address is not already in use.

        """
        try:
            user = User.objects.get(username__iexact=self.cleaned_data['username'])
        except User.DoesNotExist:
            return self.cleaned_data['username']
        raise forms.ValidationError(_("A user with that email address already exists."))
        self.cleaned_data['email'] = self.cleaned_data['username']

        return self.cleaned_data

I'm calling this through the following class:

class NoUsernameRegistrationView(RegistrationView):
    form_class = NoUsernameRegistrationForm

I get this error when I submit the from on the field username

Traceback:

File "/home/henry/Documents/Sites/Development/merz-portal/env/local/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/home/henry/Documents/Sites/Development/merz-portal/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/home/henry/Documents/Sites/Development/merz-portal/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/henry/Documents/Sites/Development/merz-portal/env/local/lib/python2.7/site-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "/home/henry/Documents/Sites/Development/merz-portal/env/local/lib/python2.7/site-packages/django_registration/views.py" in dispatch
  36.         return super(RegistrationView, self).dispatch(*args, **kwargs)

File "/home/henry/Documents/Sites/Development/merz-portal/env/local/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
  88.         return handler(request, *args, **kwargs)

File "/home/henry/Documents/Sites/Development/merz-portal/env/local/lib/python2.7/site-packages/django/views/generic/edit.py" in post
  182.         if form.is_valid():

File "/home/henry/Documents/Sites/Development/merz-portal/env/local/lib/python2.7/site-packages/django/forms/forms.py" in is_valid
  183.         return self.is_bound and not self.errors

File "/home/henry/Documents/Sites/Development/merz-portal/env/local/lib/python2.7/site-packages/django/forms/forms.py" in errors
  175.             self.full_clean()

File "/home/henry/Documents/Sites/Development/merz-portal/env/local/lib/python2.7/site-packages/django/forms/forms.py" in full_clean
  386.         self._post_clean()

File "/home/henry/Documents/Sites/Development/merz-portal/env/local/lib/python2.7/site-packages/django/forms/models.py" in _post_clean
  394.         exclude = self._get_validation_exclusions()

File "/home/henry/Documents/Sites/Development/merz-portal/env/local/lib/python2.7/site-packages/django/forms/models.py" in _get_validation_exclusions
  354.                 field_value = self.cleaned_data.get(field)

Exception Type: AttributeError at /accounts/register/
Exception Value: 'unicode' object has no attribute 'get'
HenryMehta commented 6 years ago

Silly - I have return in the wrong place. I would delete if I could

ubernostrum commented 6 years ago

This isn't a bug in django-registration; a form-level clean() method in Django must either return a dictionary of cleaned data, or return None. Your clean() method sometimes returns a string (return self.cleaned_data['username']).