Django session for setup expired User was logged in to log out, this problem hasbeen asked with someone in stackoverflow, and this is a good answer of it

That answer Update for Django 1.6, and now we worked with django 1.8.7, but it still works.

1. Add this for Handle session, in your

Handle session is not Json Serializable

SESSION_SERIALIZER = ‘django.contrib.sessions.serializers.PickleSerializer’

2. Create in your path of app, for example tree of directory like this:


And put this script in your file of

from datetime import datetime, timedelta
from django.conf import settings
from django.contrib import auth

class AutoLogout:
    def process_request(self, request):
        if not request.user.is_authenticated() :
            #Can't log out if not logged in

            if - request.session['last_touch'] > timedelta( 0, settings.AUTO_LOGOUT_DELAY * 60, 0):
                del request.session['last_touch']
        except KeyError: pass
        request.session['last_touch'] =

3. And then back again in your, and update your middleware:



# Auto logout delay in minutes
AUTO_LOGOUT_DELAY = 5 #equivalent to 5 minutes

That is automatically force logout for somebody was loggin for every 5 minuts if user not touch anything, or whatever you need to setup time it. Hopefully can help..


101 1 1 0