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

Dillon Ginley
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Dillon Ginley
Python Web Development Techdegree Graduate 26,890 Points

why am I not getting the login for this user?

from django.core.urlresolvers import reverse_lazy from django.views import generic from django.contrib.auth.forms import AuthenticationForm, UserCreationForm from django.contrib.auth import authenticate, login from django.http import HttpResponseRedirect

from . import forms

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

def form_valid(self, form):
    username = "John"
    password = "JohnPW#$#$#"
    user = authenticate(username=username, password=password)
    if user is not None:
        login(self.request, user)
        return HttpResponseRedirect(reverse_lazy("products:list")) 
    return HttpResponseRedirect(reverse_lazy("products:list"))
accounts/views.py
from django.core.urlresolvers import reverse_lazy
from django.views import generic
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm
from django.contrib.auth import authenticate, login
from django.http import HttpResponseRedirect

from . import forms


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

    def form_valid(self, form):
        username = "John"
        password = "JohnPW#$#$#"
        user = authenticate(username=username, password=password)
        if user is not None:
            login(self.request, user)
            return HttpResponseRedirect(reverse_lazy("products:list")) 
        return HttpResponseRedirect(reverse_lazy("products:list"))

1 Answer

Hi Dillion, I'm more than a little late here but, I figured this would help others in the future. I was able to find a solution from another forum so I thought I'd share it here:

 def form_valid(self, form):
# Stores the result of the validated form as "valid form"
        valid_form = super().form_valid(form)
# Pulls the username and password1 ("the variable name assigned") from the cleaned_data library
        username = form.cleaned_data['username']
        password = form.cleaned_data['password1']
# Authenticated the user with the username and password credentials
        user = authenticate(username=username, password=password)
# Logs the user in 
        login(self.request, user)
        return valid_form
#Don't forget to import "authenticate" and "login"