Skip to main content

Serve multiple sites from a single Django application

Project description


Install with pip:

pip install django-multisite

Or get the code via git:

git clone git:// django-multisite

Then run:

python install

Or add the django-multisite/multisite folder to your PYTHONPATH.

If you wish to contribute, instead run:

python develop


Replace your SITE_ID in to:

from multisite import SiteID
SITE_ID = SiteID(default=1)

Add these to your INSTALLED_APPS:


Add to your TEMPLATES loaders in the OPTIONS section:

        'DIRS': {...}
        'OPTIONS': {
            'loaders': (



Append to, in order to use a custom cache that can be safely cleared:

# The cache connection to use for django-multisite.
# Default: 'default'

# The cache key prefix that django-multisite should use.
# If not set, defaults to the KEY_PREFIX used in the defined
# CACHE_MULTISITE_ALIAS or the default cache (empty string if not set)

If you have set CACHE_MULTISITE_ALIAS to a custom value, e.g. 'multisite', add a separate backend to CACHES:

    'default': {
    'multisite': {
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
        'TIMEOUT': 60 * 60 * 24,  # 24 hours

Multisite determines the ALLOWED_HOSTS by checking all Alias domains. You can also set the MULTISITE_EXTRA_HOSTS to include additional hosts. This can include wildcards.:

# will match the single additional host

# will match any host ending ''

Development Environments

Multisite returns a valid Alias when in “development mode” (defaulting to the alias associated with the default SiteID.

Development mode is either:
  • Running tests, i.e. test
  • Running locally in settings.DEBUG = True, where the hostname is a top-level name, i.e. localhost

In order to have multisite use aliases in local environments, add entries to your local etc/hosts file to match aliases in your applications. E.g.

And access your application at or instead of the usual localhost:8000.

Domain fallbacks

By default, if the domain name is unknown, multisite will respond with an HTTP 404 Not Found error. To change this behaviour, add to

# The view function or class-based view that django-multisite will
# use when it cannot match the hostname with a Site. This can be
# the name of the function or the function itself.
# Default: None
MULTISITE_FALLBACK = 'django.views.generic.base.RedirectView

# Keyword arguments for the MULTISITE_FALLBACK view.
# Default: {}
                             'permanent': False}


If required, create template subdirectories for domain level templates (in a location specified in settings.TEMPLATES[‘DIRS’].

Multisite’s template loader will look for templates in folders with the names of domains, such as:


The template loader will also look for templates in a folder specified by the optional MULTISITE_DEFAULT_TEMPLATE_DIR setting, e.g.:


Cross-domain cookies

In order to support cross-domain cookies, for purposes like single-sign-on, prepend the following to the top of MIDDLEWARE (MIDDLEWARE_CLASSES for Django < 1.10):


CookieDomainMiddleware will consult the Public Suffix List for effective top-level domains. It caches this file in the system’s default temporary directory as effective_tld_names.dat. To change this in

MULTISITE_PUBLIC_SUFFIX_LIST_CACHE = '/path/to/multisite_tld.dat'

By default, any cookies without a domain set will be reset to allow *.domain.tld. To change this in

MULTISITE_COOKIE_DOMAIN_DEPTH = 1  # Allow only *.subdomain.domain.tld

In order to fetch a new version of the list, run: update_public_suffix_list


To run the tests:

python test



Before deploying a change, to verify it has not broken anything by running:


This runs the tests under every supported combination of Django and Python.

Project details

Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
django-multisite-1.6.0.tar.gz (103.5 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page