In django login, we can type if request.user.is_authenticated
to check if the user is logged in. But if we want to check the login status of a particular user by entering the username, we get the following:
#Python
def checkLoginStatus(username):
#do some checks here....
return True or False
Django does not keep track of the login state of a user. It keeps track of:
last_login
If you want to keep track of it, you need to do it yourself:
session_key
.request.session
(request is passed to the signal) and store it on the profile modelsession_key
to Noneis_logged_in
property that retrieves the session key for a user from its profile model and then checks if the session is still valid by getting the session from the Session
model. If it's valid, the user is logged in.You can use the query to get user that you want to check is_logged_in or not.
def checkLoginStatus(username):
if User.objects.filter(username=username).exists():
usr = User.objects.get(username=username)
if usr.is_authenticated:
return True
else:
return False
else:
print("No user exist with this {} username.".format(username))
return False
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.