Skip to main content

A simple Django middleware to refresh user sessions periodically to prevent timeout during active use

Project description

django-session-refresh

A simple Django middleware to refresh user sessions periodically (24h by default) to prevent timeout during user activity.

This middleware updates a timestamp in the session data at specified intervals, effectively extending the session's lifetime as long as the user is active.

  • Since this middleware depends on user information, it should be placed after Django's AuthenticationMiddleware in the middleware stack.

Installation

  1. Run pip install django-session-refresh
  2. Add django_session_refresh.middleware.SessionRefreshMiddleware to settings.py ass bellow
MIDDLEWARE = [
    ...
    'django.contrib.auth.middleware.AuthenticationMiddleware',  # Django default AuthenticationMiddleware
    'django_session_refresh.middleware.SessionRefreshMiddleware',  # django-session-refresh
    ...
]

Configuration (optional)

The dictionary settings bellow are optional and if not defined in settings.py the default values will be used.

DJANGO_SESSION_REFRESH = {
    # The interval in seconds to refresh the session
    'REFRESH_INTERVAL': 86400,  # 24 hours in seconds

    # The name of the session key to store the last refresh timestamp
    'SESSION_KEY_NAME': 'django_session_refreshed_at',

    # The name of the request attribute to store session refresh information
    'REQUEST_ATTR_NAME': 'django_session_refresh',

    # Skips session refresh for is_staff users
    'SKIP_STAFF_USERS': False,

    # Skips session refresh for is_superuser users
    'SKIP_SUPERUSER_USERS': True,

    # Skips session refresh for static and media file requests
    'SKIP_STATIC_AND_MEDIA': True,

    # Skips session refresh for unauthenticated users
    'SKIP_UNAUTHENTICATED_USERS': True,
}

Request features

After a successful session refresh, the middleware adds the following dictionary attribute to the request object:

  • request.django_session_refresh['refreshed']: A boolean indicating whether the session was refreshed during the current request.
  • request.django_session_refresh['refreshed_at']: A datetime object representing the timestamp when the session was last refreshed.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_session_refresh-0.3.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_session_refresh-0.3.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file django_session_refresh-0.3.0.tar.gz.

File metadata

  • Download URL: django_session_refresh-0.3.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for django_session_refresh-0.3.0.tar.gz
Algorithm Hash digest
SHA256 89b6e0d2a765e71e1dfb1009f9c1aa37a23560bb817ee953a467684bdf953fb3
MD5 560d0e4bd12ff3557c031206c635b8aa
BLAKE2b-256 a3be65b705c59036aa53b2f8a719f7bafb72c62809335f42f24863c3c60a845e

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_session_refresh-0.3.0.tar.gz:

Publisher: publish.yml on amilnosredna/django-session-refresh

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_session_refresh-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_session_refresh-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9ddaa4a304a2430d54b6ab610259043aeafc08d0513fba18f53503dd8f6c2078
MD5 b0151df1f0496af66f7ec44de631f838
BLAKE2b-256 4bea3b07315f54fb702efb016f63615347fd1fb73be21869ea85251814b7b7d2

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_session_refresh-0.3.0-py3-none-any.whl:

Publisher: publish.yml on amilnosredna/django-session-refresh

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page