How to expire session due to inactivity in Django?

0 votes
How to expire session due to inactivity in Django?
Aug 13 in Python by kartik
• 27,780 points
168 views

1 answer to this question.

0 votes

Hello @kartik,

Expire the session on browser close with the SESSION_EXPIRE_AT_BROWSER_CLOSE setting. Then set a timestamp in the session on every request like so.

request.session['last_activity'] = datetime.now()

and add a middleware to detect if the session is expired. something like this should handle the whole process...

from datetime import datetime
from django.http import HttpResponseRedirect

class SessionExpiredMiddleware:
    def process_request(request):
        last_activity = request.session['last_activity']
        now = datetime.now()

        if (now - last_activity).minutes > 10:
            # Do logout / expire session
            # and then...
            return HttpResponseRedirect("LOGIN_PAGE_URL")

        if not request.is_ajax():
            # don't set this for ajax requests or else your
            # expired session checks will keep the session from
            # expiring :)
            request.session['last_activity'] = now

Then you just have to make some urls and views to return relevant data to the ajax calls regarding the session expiry.

when the user opts to "renew" the session, so to speak, all you have to do is set requeset.session['last_activity'] to the current time again

Hope this helps!!

Thank you!!

answered Aug 13 by Niroj
• 59,780 points

Related Questions In Python

0 votes
1 answer

How to switch pages using Ajax in Django?

You should use django-pjax which is built exactly for ...READ MORE

answered Oct 9, 2018 in Python by aryya
• 7,380 points
415 views
0 votes
1 answer

How to get all related Django model objects in Python?

This actually gives you the property names ...READ MORE

answered Nov 14, 2018 in Python by Nymeria
• 3,520 points

edited Dec 18, 2018 by Nymeria 2,956 views
0 votes
1 answer

How to run django unit-tests on production database in Python?

In case someone googles here searching for ...READ MORE

answered Nov 28, 2018 in Python by Nymeria
• 3,520 points
360 views
0 votes
1 answer

How to generate urls in django?

If you need to use something similar ...READ MORE

answered Apr 12, 2019 in Python by SDeb
• 13,250 points
108 views
0 votes
1 answer

How to temporarily disable a foreign key constraint in MySQL?

Hello @kartik, To turn off foreign key constraint ...READ MORE

answered Jun 23 in Python by Niroj
• 59,780 points
192 views
0 votes
1 answer

How do I use Django templates without the rest of Django?

Hello @kartik, Let's say you have this important ...READ MORE

answered Jun 23 in Python by Niroj
• 59,780 points
83 views
0 votes
1 answer

How to update Django session variable in javascript?

Hello @kartik, You can do this via Ajax. ...READ MORE

answered Jul 30 in Python by Niroj
• 59,780 points
377 views
0 votes
1 answer

How to query as GROUP BY in django?

Hii, Django does not support free group by ...READ MORE

answered Apr 23 in Python by Niroj
• 59,780 points
132 views