简体   繁体   中英

How to login automatically after signup in django customuser?

So I followed a tutorial online about creating a customuser registration/login in django. Now both works fine. when user logs in from login page it uses this link LOGIN_REDIRECT_URL = 'profile' . But for a user that just registered I want it to be logged in automatically and also redirect to suggestion as in the view not to the profile. But I don't know how to login the user from this view.

I tried to add to use login() here but I have no idea how to use it here.

class SignUp(generic.CreateView):
    form_class = CustomUserCreationForm
    template_name = 'signup.html'
    success_url = reverse_lazy('suggestion')
    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 HttpResponseRedirect(self.get_success_url)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM