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-
How to Open Ports in Windows?
207 2
-
How to Grant Windows Administrator Privileges?
184 0
-
How to install Webmin on Ubuntu?
803 2
-
Linux SSH Terminal Commands and Explanations
682 3
-
CentOS Stream 10 Update Commands
768 1
-
CentOS Stream 9 Update Commands
1112 1
-
CentOS Stream 8 Update Commands
621 0
-
What is FreeBSD?
765 2
-
Out: 452 4.3.1 Insufficient system storage
708 2
-
Turning Off Automatic Updates in Windows Operating Systems
822 0