X
X

Select Your Currency

Türk Lirası $ US Dollar
X
X

Select Your Currency

Türk Lirası $ US Dollar
Yıllık Satın Alımlarda %15 İndirim: Şimdi Tasarruf Edin! Detaylı Bilgi İçin Tıklayın!

Knowledge Base

HomepageKnowledge BaseGeneralDjango Session Timeout

Django Session Timeout

In your Django projects, you can check each request and response transaction with the middleware to log in again or provide information according to the session's timeout, and log out if the transaction has not been completed for more than the specified time.

/projectfolder/application/middleware.py
import time

from django.contrib import messages
from django.contrib.auth import logout
from django.utils.translation import ugettext as _

SESSION_TIME_TIME_DURATION = 10


class SessionTimeoutControl:

def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):

if request.user.is_authenticated:
current_time = int(time.time())

try:
request.session['last_activity']
except:
request.session['last_activity'] = current_time

if (current_time - request.session['last_activity']) >= SESSION_TIME_DURATION:
logout(request)
messages.add_message(request, messages.ERROR, _('Your session has timed out!'))

else:
request.session['last_activity'] = current_time

response = self.get_response(request)
return response

projectfolder/projectfolder/settings.py
MIDDLEWARE = ​​[
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'application.middleware.SessionTimeTimeControl'
]

You can click for our professional Linux hosting packages suitable for every budget.

Can't find the information you're looking for?

You have examined the knowledge base in detail, but if you cannot find the information you need,

Create a Support Ticket
Did you find it useful?
(100 times viewed. / 0 people found helpful.)