Home > OS >  How to check if an existing specific user (say techxhelp) is logged in and then logout that specific
How to check if an existing specific user (say techxhelp) is logged in and then logout that specific

Time:02-21

I want to do something like this in views.

This is not a perfect code. I am just giving an example of what is my requirement.

def home(request):
    if request.user.techxhelp is_logged_in:
            logout(techxhelp)
    else:
            pass

I tried searching Google and different sites but I found nothing. If anyone knows the solution, I shall be very thankful.

CodePudding user response:

Simply determine the Id of the specific user you are intending to logout, then you can implement something like:

from django.contrib.auth import logout

BAD_USER = 1337 # assuming the user's Id is '1337'

def home(request):
    user = request.user
    if user.id == BAD_USER and user.is_authenticated(): # might differ depending on your auth-system.
            logout(request)
    # ...
    else:
            pass

Alternate method as requested in comments:

from django.contrib.auth import logout

BAD_USER = "techxhelp" # assuming the user's username is 'techxhelp'

def home(request):
    user = request.user
    if user.is_authenticated() and user.username == BAD_USER: # might differ depending on your auth-system.
            logout(request)
    # ...
    else:
            pass
  • Related