0

I am trying to do force logout of django user who is not longer active.I need to be able to force user logout by a username. After doing some research I have found that django has an option to logout by using logout(request) but it no giving me option to select username based logout. Anyone has any idea? Thanks

I have tried to test this following code for my problem which was taken from How to force user logout in django? In my model:

from django.contrib.auth.models import User
from django.db import models
from datetime import datetime

class MyUser(User):
force_logout_date = models.DateTimeField(null=True, blank=True)

def force_logout(self):
    self.force_logout_date = datetime.now()
    self.save()

Then I have added this part in my views.py:

from myapp.models import MyUser
MyUser.objects.get(username='johndoe').force_logout()

But I have following error now:

MyUser matching query does not exist.

Request Method:     POST
Request URL:    http://localhost:8000/datatableuser/?    
typeoftask=edit&username=xyzstaff
Django Version:     1.8.11
Exception Type:     DoesNotExist
Exception Value:    

MyUser matching query does not exist.

Exception Location:     /usr/local/lib/python2.7/dist-  
packages/django/db/models/query.py in get, line 334
Python Executable:  /usr/bin/python
Python Version:     2.7.6
Python Path:    

['/home/paul/Desktop/djangoproject/peptidetrackerdatabase/src',
'/usr/local/lib/python2.7/dist-packages/setuptools-20.3.1-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/pip-8.1.1-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/Sphinx-1.4-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/imagesize-0.7.0-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/alabaster-0.7.7-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/Babel-2.2.0-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/snowballstemmer-1.2.1-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/docutils-0.12-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/Pygments-2.1.3-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/Jinja2-2.8-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/MarkupSafe-0.23-py2.7-linux-x86_64.egg',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PILcompat',
'/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/pymodules/python2.7',
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client']

Server time:    Thu, 14 Apr 2016 22:12:42 -0700    enter code here
Community
  • 1
  • 1
Paul85
  • 647
  • 1
  • 11
  • 27

1 Answers1

0

It mostly depends on how you do the authentication. It is impossible to give you an answer without knowing that exactly.

However, assuming that you are using the default Django authentication, this answer should still be valid:

How to force user logout in django?

Community
  • 1
  • 1
R Bazhenov
  • 178
  • 8
  • Hi thanks for your response. I saw this solution but couldn't figure out yet how to implement it because my Session.objects.all() is always return empty list and I don't know why – Paul85 Apr 15 '16 at 05:02
  • @Paul85 Please check if 'django.contrib.sessions' is in your installed apps, 'django.contrib.sessions.middleware.SessionMiddleware' in your activated middlewares, users can log in and that migrations are applied. In addition, please verify manually in your database that the session table exists and it is not empty. – R Bazhenov Apr 15 '16 at 05:17
  • I have check and my settings.py is ok. – Paul85 Apr 15 '16 at 05:27