Package: django-auto-logout
Auto logout a user after specific time in Django.Works with Python >= 3.7, Django >= 3.0.Installationpipinstalldjango-auto-logoutAppend tosettingsmiddlewares:MIDDLEWARE=[...'django_auto_logout.middleware.auto_logout',]NoteMake sure that the following middlewares are used before doing this:django.contrib.sessions.middleware.SessionMiddlewaredjango.contrib.auth.middleware.AuthenticationMiddlewaredjango.contrib.messages.middleware.MessageMiddlewareLogout in case of idleLogout a user if there are no requests for a long time.Add tosettings:AUTO_LOGOUT={'IDLE_TIME':600}# logout after 10 minutes of downtimeor the same, but withdatetime.timedelta(more semantically):AUTO_LOGOUT={'IDLE_TIME':timedelta(minutes=10)}The user will log out the next time the page is requested.
SeeREDIRECT_TO_LOGIN_IMMEDIATELYto log out right after the idle-time has expired
(and redirect to login page).REDIRECT_TO_LOGIN_IMMEDIATELY after the idle-time has expiredUse theREDIRECT_TO_LOGIN_IMMEDIATELYoption
if you want to redirect the user to the login page
immediately after the idle-time expires:fromdatetimeimporttimedeltaAUTO_LOGOUT={'IDLE_TIME':timedelta(minutes=10),'REDIRECT_TO_LOGIN_IMMEDIATELY':True,}This requires a client-side script, so you should
modify yourcontext_processorsinsettings.py:TEMPLATES=[{'BACKEND':'django.template.backends.django.DjangoTemplates','DIRS':[],'APP_DIRS':True,'OPTIONS':{'context_processors':['django.template.context_processors.debug','django.template.context_processors.request','django.contrib.auth.context_processors.auth','django.contrib.messages.context_processors.messages',# !!! Add this !!!'django_auto_logout.context_processors.auto_logout_client',],},},]And add this to your templates (will add a redirect script to your html):{{redirect_to_login_immediately}}If you want to use this in your JavaScript code, following template variables may be useful:varsessionEnd={{seconds_until_session_end}};varidleEnd={{seconds_until_idle_end}};REDIRECT_TO_LOGIN_IMMEDIATELYworks withSESSION_TIMEtoo.Limit session timeLogout a user after 3600 seconds (hour) from the last login.Add tosettings:AUTO_LOGOUT={'SESSION_TIME':3600}or the same, but withdatetime.timedelta(more semantically):AUTO_LOGOUT={'SESSION_TIME':timedelta(hours=1)}NoteSeeREDIRECT_TO_LOGIN_IMMEDIATELYoption
if you want to redirect user to the login page
right after the idle-time has expired.Show messages when logging out automaticallySet the message that will be displayed after the user automatically logs out of the system:AUTO_LOGOUT={'SESSION_TIME':3600,'MESSAGE':'The session has expired. Please login again to continue.',}It usesdjango.contrib.messages. Don’t forget to display messages in templates:{% for message in messages %}<divclass="message {{ message.tags }}">{{ message }}</div>{% endfor %}Notemessagestemplate variable provides bydjango.contrib.messages.context_processors.messagescontext processor.SeeTEMPLATES-OPTIONS-context_processorsin yoursettings.pyfile.Combine configurationsYou can combine previous configurations. For example, you may want to logout a user
in case of downtime (5 minutes or more) and not allow working within one session
for more than half an hour:fromdatetimeimporttimedeltaAUTO_LOGOUT={'IDLE_TIME':timedelta(minutes=5),'SESSION_TIME':timedelta(minutes=30),'MESSAGE':'The session has expired. Please login again to continue.','REDIRECT_TO_LOGIN_IMMEDIATELY':True,}