You need to authenticate(**credential)
the user when the user having no password.
django.contrib.auth.authenticate()
is to authenticate a given username and password.
So you should create your own authentication backend given only username. Basically the only authentication you need to do is to check if the user is in the database. But it is pretty obvious that the user is in the database. So alternatively, you can just fake the authentication process by setting the user.backend = 'django.contrib.auth.backends.ModelBackend'
directly.
Followed by login()
to login a given user.
When you’re manually logging a user in, you must successfully authenticate the user with 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.
def autologin(request):
# ...
# ...
# ...
new_user = create_user(
username=username,
email=email,
first_name=first_name,
last_name=last_name)
user = authenticate(new_user)
if user.is_authenticated():
login(request, user)
return render(request, 'template.html')