Contrib¶
Manually Authenticating a User:¶
django.contrib.auth defines a method named login, with the following signature (in the file __init__.py):
def login(request, user):
"""
Persist a user id and a backend in the request. This way a user doesn't
have to reauthenticate on every request.
"""
login takes request and the user object. However, passing a standard user object and a request, doesn’t log the user in, when logging in manually, because authenticate() sets an extra argument on the user object, that is used for logging in.
Solution:
When loggining in the user manually, first use the authenticate method.:
user_auth = authenticate(username=new_user.username, password=pw1)
login(request, user_auth)