Skip to content
Cross domain session sharing for projects using the same Django application on multiple domains
Branch: master
Clone or download
Pull request Compare This branch is 1 commit behind vit-ivanov:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


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 (start's 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.

You can’t perform that action at this time.