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 trialmarcelolima
1,318 PointsHow to login the user after a successful a signup?
I'm using the method 'form_valid' to use the data from the validated form to perform the authentication, login and finally the redirection that the question asks, but the solution is wrong. Can someone help me please?
from django.core.urlresolvers import reverse_lazy
from django.views import generic
from django.shortcuts import redirect
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):
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
redirect(success_url)
1 Answer
marcelolima
1,318 PointsFound the issues:
1 - form_valid is an inherited method that performs form.save() and redirects. As the question asked to do a login between the user registration (save) and the redirection, I included the code to save at the beginning of the method and the code to redirect at the end (after the login).
2 - on the form, there is no 'password' field, I had to use 'password1' instead