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

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,716 Points

Wasting time on this challenge... please give better diagnostic message or at least a better hint.

I read the pages that were in the hint.

I pass part 1 of the challenge, but on part two is a mystery and I have tried MANY permutations.

Maybe the form is customized? I can't look at the data that comes back from the form.

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

from . import forms


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

    def get(self, request, *args, **kwargs):
        return super().get(request, *args, **kwargs)

    def form_valid(self,form):
        username = form.cleaned_data.get('username')
        password = form.cleaned_data.get('password')
        user = authenticate(username=username, password=password)
        if user:
            login(self.request,user)
        return super().form_valid(form)
Jeff Muday
Jeff Muday
Treehouse Moderator 28,716 Points

I solved my problem. I think it was affected by putting the call to the super().form_valid(form) first... and making the assumption that the form password verification came from cleaned_data 'password1' (though I had tried that in my earlier approach and still got the "Bummer..." diagnostic).

I also removed the 'if user:' that would only login if authenticate function returned anything other than None.

See partial solution below.

def form_valid(self, form):
        valid = super().form_valid(form)
        username = form.cleaned_data.get('username')
        password = form.cleaned_data.get('password1') # assuming standard password1, password2 validation
        valid_user = authenticate(username=username, password=password)
        login(self.request, valid_user)
        return valid
Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Nice solution, but I'm surprised this passed. The challenge does not seem to test the case of a user that doesn't pass authenticate(). Most solutions include a if valid_user is not None before calling login().

Tagging Kenneth Love to review if a failing form case needs to be included in the challenge testing.

2 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Hey Chris Freeman, that's expected behavior since this is a sign up view that logs users in automatically. You know the user is valid because you just created it. If this was a normal login view, though, you'd have to check that it was a valid user before returning the logged-in session (or even being able to create it)

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

I see your point. It's my validation brain that struggles with assumptions even if it's correct by construction.

What if the super().form_valid() fails, would the authenticate() and login() still run correctly?

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,716 Points

Off topic-- I have been impressed by the Code Challenges and at some point would like to see Kenneth Love or Chris Freeman on a "teacher/developer interview" segment about some of the infrastructure behind the overarching learning management system and in particular, the implementation of the Code Challenges.

When I was taking my first course, I became worried that Code Challenges were only a moderately simple pattern matching algorithm looking only at the code. But, to my surprise, I quickly learned that the code we wrote was indeed executed and "operational" and I could write the code in any valid syntactic manner... any valid variable names, helper functions, etc. (running much more like a tests.py or Python "unittest" framework) the pass/fail condition being all about expected output or "interrogation" of particular data structures.

And, as I started playing with Ruby, HTML, CSS courses, these Code Challenges are just as seamless as the Python.

Which has me wondering-- How do you achieve all this behind the scenes?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

I would love to learn more about it too!! I have only surmised my understanding of how the challenge checker works through a general knowledge of validation checkers and reading hundreds of related questions in the Community forum and clarifying answers by Kenneth Love.