Search code examples
pythondjangodjango-registration

Django login form not working for custom user model


I am using Django 1.8 and have implemented a custom user model. The user registration piece is 100% functional; I can submit a form and verify that users are created. But I am struggling with the user login process.

The login form renders just fine, but when I enter a username and password that I have verified is registered (verified via the Django admin) I get to the HttpResponse('Form is invalid') message.

I have been stuck on this for a day or two. Any suggestions are greatly appreciated!

accounts/views.py

from django.views.generic import FormView
from django.contrib.auth import authenticate, login
from django.shortcuts import render

from accounts.forms import CustomUserCreationForm, CustomUserLoginForm
from accounts.models import CustomUser


class CustomUserCreateView(FormView):
    form_class = CustomUserCreationForm
    template_name = 'registration/registration_form.html'
    success_url = '/connections/'

    def form_valid(self, form):
        form.save()
        return super(CustomUserCreateView, self).form_valid(form)


class CustomUserLoginView(FormView):
    form_class = CustomUserLoginForm
    template_name = 'registration/login.html'
    success_url = '/success/'

    def get(self, request, *args, **kwargs):
        form = self.form_class(initial=self.initial)
        return render(request, self.template_name, {'form':form})

    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST)
        if form.is_valid():
            user = authenticate(
                username=form.cleaned_data['email'],
                password=form.cleaned_data['password'],
                )
            if user is not None:
                if user.is_active:
                    login(request, user)
                    return HttpResponseRedirect(success_url)
                else:
                    return HttpResponse('User is not active') # TEMP
            else:
                return HttpResponse('User does not exist') # TEMP
        else:
            return HttpResponse('Form is invalid') # TEMP

accounts/forms.py

from django import forms
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm

from .models import CustomUser


class CustomUserLoginForm(AuthenticationForm):
    model = CustomUser
    # TODO - need to provide error message when no user is found


class CustomUserCreationForm(UserCreationForm):
    password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
    password2 = forms.CharField(label='Confirm Password', widget=forms.PasswordInput)

    class Meta(UserCreationForm.Meta):
        model = CustomUser
        fields = ('first_name', 'last_name', 'email', 'mobile_number')

    def clean_password2(self):
        # Check that the two password entries match
        password1 = self.cleaned_data.get('password1')
        password2 = self.cleaned_data.get('password2')

        if password1 and password2 and password1 != password2:
            raise forms.ValidationError('Passwords do not match!')
        return password2

    def save(self, commit=True):
        # Save the provided password in hashed format
        user = super(UserCreationForm, self).save(commit=False)
        user.set_password(self.cleaned_data['password1'])
        if commit:
            user.save()
        return user

Solution

  • This answer ultimately led me to the fix.

    In the 'post' method I needed to change the line from:

    form = self.form_class(request.POST)
    

    to:

    form = self.form_class(data=request.POST)
    

    Finally, my CustomUserLoginView looks like this:

    class CustomUserLoginView(FormView):
        form_class = AuthenticationForm
        template_name = 'registration/login.html'
        success_url = '/connections/'
    
        def get(self, request, *args, **kwargs):
            form = self.form_class(initial=self.initial)
            return render(request, self.template_name, {'form':form})
    
        def post(self, request, *args, **kwargs):
            form = self.form_class(data=request.POST)
            if form.is_valid():
                user = authenticate(
                    username=form.cleaned_data['username'],
                    password=form.cleaned_data['password'],
                    )
                if user is not None:
                    if user.is_active:
                        login(request, user)
                        return HttpResponseRedirect(self.success_url)
                    else:
                        return HttpResponse('User is not active') # TEMP
                else:
                    return HttpResponse('User does not exist') # TEMP
            else:
                return HttpResponse('Form is not valid') # TEMP