Python Django Authentication Authentication SignUpView

Simon Amz
Simon Amz
4,605 Points

Login in the same time than the registration

Hi there,

I need to login in the same time than the registration.

I thus used methods authenticate and login, in order to proceed to the authentication and the login directly after validating the sign up form.

However, there still is an error that I have to use 'authenticate' and 'login' which I did.

Do you have any idea?

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

from . import forms


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


    def form_valid(self, form):
        form.save()
        username = self.request.POST['username']
        password = self.request.POST['password1']
        user = authenticate(username=username, password=password)
        login(self.request, user)
        return super().form_valid(form)

4 Answers

Simon Amz
Simon Amz
4,605 Points

Unfortunately, it didn't in my account.

Besides, I tried this:

from django.contrib.auth import authenticate, login
from django.core.urlresolvers import reverse_lazy
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):
        form.save()
        username = self.cleaned_data['username']
        password = self.cleaned_data['password1']
        user = authenticate(self, request, username=username, password=password)
        login(self.request, user)
        return super().form_valid(form)

But it still says that "Oops! It looks like Task 1 is no longer passing."

When I go back to the task 1, I noticed that, the error message is " Bummer! Didn't get the right redirect with a successful POST"

Henrik Christensen
Henrik Christensen
35,790 Points

It works fine for me, but the problem is at your form.save() because you should be calling super().form_valid(form) there and again after the user has been logged in - like this:

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

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):
        valid = super().form_valid(form)
        username = self.request.POST['username']
        password = self.request.POST['password1']
        user = authenticate(username=username, password=password)
        login(self.request, user)
        return valid
Simon Amz
Simon Amz
4,605 Points

finally got it. I was returning the super().form_valid(form), without calling it at first.

Thanks for your help