I am using Django-Python. Is there any way to set session never expire?
I don't know about infinite sessions, but if you use your session middleware and use a huge AUTOLOGOUT time, it will work.
does 100 years sound ok?
on settings.py
# time in minutes
# 60min*24hours*365days*100years
AUTO_LOGOUT = 52560000
Create a middleware and add it to settings.py in the middleware section
from datetime import datetime, timedelta
from django.conf import settings
from django.contrib import auth
from django.utils.translation import ugettext as _
class AutoLogout:
def process_request(self, request):
if not request.user.is_authenticated():
# Can't log out if is not logged in
return
try:
# last click
last_touch = datetime.strptime(request.session['last_touch'], "%Y-%m-%d %H:%M:%S.%f")
# getting auto logout time
auto_logout_time = settings.AUTO_LOGOUT
try:
if datetime.now() - last_touch > timedelta(0, auto_logout_time * 60, 0):
del request.session['last_touch']
auth.logout(request)
request.session['warning'] = unicode(_("You have been logged out"))
return
except Exception as e:
# It should not reach this point
auth.logout(request)
del request.session['last_touch']
request.session['warning'] = unicode(_("You have been logged out"))
except KeyError:
pass
request.session['last_touch'] = str(datetime.now())
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments