[Answered ]-Django register

2👍

You need to use authenticate() first, before calling login().

from django.contrib.auth import authenticate, login
if(form.is_valid()):
    form.save()
    new_user = authenticate(username=form.cleaned_data.get('username'), 
                            password= form.cleaned_data.get('password'))
    login(request,new_user)
    return redirect('/dashboard/')

From Django docs:

When you’re manually logging a user in, you must call authenticate() before you call login(). authenticate() sets an attribute on the User noting which authentication backend successfully authenticated that user (see the backends documentation for details), and this information is needed later during the login process. An error will be raised if you try to login a user object retrieved from the database directly.

Leave a comment