Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i kick off a user who has logged-in in django?

Tags:

django

I am using Django.

How can i kick off a user who has logged-in?

or

How can i get the user's session and kill it?

like image 363
Pin Zhang Avatar asked Feb 27 '23 18:02

Pin Zhang


1 Answers

If you can access the shell, then:

$ ./manage.py shell

In [1]: from django.contrib.sessions.models import Session

In [2]: for s in Session.objects.all():
   ...:     data = s.get_decoded()
   ...:     if data.get('_auth_user_id', None) == YOUR_USER_ID:
   ...:         s.delete()
   ...:         
   ...:         

Replace YOUR_USER_ID with the ID of a user to be kicked.

The key '_auth_user_id' may differ from version to version, I suppose, but that worked for me. To check if you have the same, just print out some of s.get_decoded() and look at the output.

like image 182
dmedvinsky Avatar answered Mar 08 '23 00:03

dmedvinsky