django-auto-logout
Auto logout a user after specific time in Django.
Works with Python >= 3.7, Django >= 3.0.
Installation
pip install django-auto-logout
Append to settings middlewares:
MIDDLEWARE = [
...
'django_auto_logout.middleware.auto_logout',
]
Logout in case of idle
Logout a user if there are no requests for a long time.
Add to settings:
AUTO_LOGOUT = {'IDLE_TIME': 600} # logout after 10 minutes of downtime
or the same, but with datetime.timedelta (more semantically):
AUTO_LOGOUT = {'IDLE_TIME': timedelta(minutes=10)}
The user will log out the next time the page is requested. See REDIRECT_TO_LOGIN_IMMEDIATELY to log out right after the idle-time has expired (and redirect to login page).
REDIRECT_TO_LOGIN_IMMEDIATELY after the idle-time has expired
Use the REDIRECT_TO_LOGIN_IMMEDIATELY option if you want to redirect the user to the login page immediately after the idle-time expires:
from datetime import timedelta
AUTO_LOGOUT = {
'IDLE_TIME': timedelta(minutes=10),
'REDIRECT_TO_LOGIN_IMMEDIATELY': True,
}
This requires a client-side script, so you should modify your context_processors in settings.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:
var sessionEnd = {{ seconds_until_session_end }};
var idleEnd = {{ seconds_until_idle_end }};
REDIRECT_TO_LOGIN_IMMEDIATELY works with SESSION_TIME too.
Limit session time
Logout a user after 3600 seconds (hour) from the last login.
Add to settings:
AUTO_LOGOUT = {'SESSION_TIME': 3600}
or the same, but with datetime.timedelta (more semantically):
AUTO_LOGOUT = {'SESSION_TIME': timedelta(hours=1)}
Show messages when logging out automatically
Set 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 uses django.contrib.messages. Don’t forget to display messages in templates:
{% for message in messages %}
<div class="message {{ message.tags }}">
{{ message }}
</div>
{% endfor %}
Combine configurations
You 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:
from datetime import timedelta
AUTO_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,
}
Metadata
Release files for django-auto-logout 0.5.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django-auto-logout-0.5.1.tar.gz | 5.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_auto_logout-0.5.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.7 kB
Release files / django-auto-logout-0.5.1.tar.gz
| Download URL | django-auto-logout-0.5.1.tar.gz |
|---|---|
| Size | 5.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cf5622d60ae42c9e799ab32948b98ce007be570ec34e718e5c47340793dd76f9
|
|
BLAKE2b-256 checksum How to use checksums |
397fdc29705c97b87113a86269ce25e1e652ff50a0cf6e5cb05ed7d4d338dcac
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.6
|
Release files / django_auto_logout-0.5.1-py3-none-any.whl
| Download URL | django_auto_logout-0.5.1-py3-none-any.whl |
|---|---|
| Size | 6.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0a5f7b88b78fdcc14d7359654d61f1ad264d0028b7fdbd2cb64479ab05ddbb10
|
|
BLAKE2b-256 checksum How to use checksums |
7f51b9d796bd72132d3f142d82f8d213d40ba3cc8423af810a2a59200fbcdc57
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.6
|