Skip to main content

Django session sharing across multiple domains running same application

Project description


django-shared-session is a tool that enables cross site session sharing, which can be useful when running the same Django application on different domains (for example due to different language mutations). This library can be used for sharing login information as well as session data for both authenticated and anonymous users. If all you need is to share login information, please consider using some of single sign-on (SSO) solutions which could be better for this specific use case.

This tool is only useful when you are running your application on different domains, not just subdomains. Subdomains can be handled with cookies path set to .domain.tld (starts with dot).

This project is inspired by django-xsession, but uses a different approach to session sharing which sets the cookie on server-side and thus does not require reloading the page.

How it works

  1. User visits one of the configured sites
  2. Session key is encrypted and included in the HTML file. This file contains <script> tags linking to all configured sites with encrypted session key (as part of the file name).
  3. Browser requests the script files
  4. On destination domain the session key is decrypted from payload and saved to cookies

Example HTML snippet:

<script src="" async></script>
<script src="" async></script>

Encrypted payload (containing session key, timestamp, source and destination hostname) in base64 is part of the filename itself. Destination server checks the timestamp to prevent replay attacks.


pip install django-shared-session

This tool accesses request inside template, so please make sure you have RequestContext enabled in your template's engine context processors.


Add shared_session to INSTALLED_APPS and set shared session domains in Django settings file. Then add shared_session.urls to your urlconf.

    # ...


import shared_session

urlpatterns = [
    url(r'^shared-session/', shared_session.urls),  # feel free to change the base url

In order to share sessions with configured sites you also need to use {% shared_session_loader %} in your base template.


{% load shared_session %}

<!DOCTYPE html>
<html lang="en">
        <meta charset="UTF-8">
        {% shared_session_loader %}

If you want to share sessions also in Django admin interface, you can overwrite admin/base_site.html and include the loader.

Advanced options

SHARED_SESSION_ALWAYS_REPLACE – Always replace session cookie, even if the session is not empty. (default: False) SHARED_SESSION_TIMEOUT – Expiration timeout. Session needs to be delivered to destination site before this time. (default: 30)


Signal session_replaced is triggered when target's site session cookie was changed or created. You can connect your own handlers to run additional functions.

from shared_session import signals
import logging

def log_session_replace(sender, **kwargs):'%s session replaced' % kwargs.get('dst_domain'))



This software is licensed under MPL 2.0.

Project details

Download files

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

Files for django-shared-session, version 0.5.3
Filename, size File type Python version Upload date Hashes
Filename, size django-shared-session-0.5.3.tar.gz (6.1 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page