Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django Authentication Authentication SignUpView

SignUp view problem

Hi all,

I am getting a "Bummer: Try again!" error. I can't spot the error.

accounts/views.py
from django.core.urlresolvers import reverse_lazy
from django.contrib.auth import authenticate, login
from django.views import generic
from . import forms


class SignUp(generic.CreateView):
    form_class = forms.UserCreateForm
    template_name = 'accounts/signup.html'
    success_url = reverse_lazy("products:list")

    def form_valid(self, form):
        saved_form = super().form_valid(form)
        usr = form.cleaned_data['username'] 
        pswd = form.cleaned_data['password2'] # use confirmation field
        user = authenticate(username=usr, password=pswd)
        if user is not None:
            if user.is_active:
                login(self.request, user)
        return saved_form

1 Answer

You issue might be in your if conditions? I'm not sure as I didn't try to do it this way, but maybe one of those if conditions is failing, like for example the is_active attribute, which would prevent the user from being logged in.

The code I used for the challenge assumes that if the registration form is valid then the user is also valid and simply logs them in directly.

def form_valid(self, form):
    data = super().form_valid(form)
    user = authenticate(
        username=form.cleaned_data["username"],
        password=form.cleaned_data["password2"],
    )
    login(self.request, user)
    return data