[Answer]-Global name user_id not defined in Django?

1👍

Doing global user_id does not declare a global variable. It just tell python that this variable is a global scope variable. I don’t like that you are trying to store user_id in global variable. Always access the current user id using request.user.id in your view.

This piece of code in your log_backend view is also wrong:

global user_id 
user_id = request.user.id # user does not exists in the request because user was trying to logged in

Just remove it you don’t need it. Also you need to do login(request, user) after authenticate because authenticate function is just to check username and password supplied is correct.

user = authenticate(username=username1, password=password1)
login(request, user)

Leave a comment