Pin Zhang
Pin Zhang

Reputation: 300

How can i kick off a user who has logged-in in 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?

Upvotes: 4

Views: 4291

Answers (1)

dmedvinsky
dmedvinsky

Reputation: 8356

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.

Upvotes: 8

Related Questions