Skip to main content

Serve multiple sites from a single Django application

Project description

pypi actions codecov downloads Maintainability Code Style

README

Python 3.11+ Django 4.2+

Older versions of Django are supported by the original django-multisite project.

Installation

Install with pip:

pip install django-multisite2

Replace your SITE_ID in settings.py to:

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

add to INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    'django.contrib.sites',
    'multisite',
    ...
]

Edit settings.py MIDDLEWARE:

MIDDLEWARE = (
    ...
    'multisite.middleware.DynamicSiteMiddleware',
    ...
)

Using a custom cache

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

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

# 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)
CACHE_MULTISITE_KEY_PREFIX = ''

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

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

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 settings.py:

# 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: {}
MULTISITE_FALLBACK_KWARGS = {'url': 'http://example.com/',
                             'permanent': False}

Templates

This feature has been removed in version 2.0.0.

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:

templates/example.com

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

templates/multisite_templates

Cross-domain cookies

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

MIDDLEWARE = (
    'multisite.middleware.CookieDomainMiddleware',
    ...
)

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 settings.py:

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 settings.py:

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

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

manage.py update_public_suffix_list

Post-migrate signal: post_migrate_sync_alias

The post-migrate signal post_migrate_sync_alias is registered in the apps.py. post_migrate_sync_alias ensures the domain in multisite’s Alias model is updated to match that of django’s Site model. This signal must run AFTER any post-migrate signals that manipulate Django’s Site model. If you have an app that manipulates Django’s Site model, place it before multisite in settings. INSTALLED_APPS. If this is not possible, you may configure multisite to not connect the post-migrate signal in apps.py so that you can do it somewhere else in your code.

To configure multisite to not connect the post-post_migrate_sync_alias in the apps.py, update your settings:

MULTISITE_REGISTER_POST_MIGRATE_SYNC_ALIAS = False

With the settings attribute set to False, it is your responsibility to connect the signal in your code. Note that if you do not sync the Alias and Site models after the Site model has changed, multisite may not recognize the domain and switch to the fallback view or raise a Http404 error.

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. manage.py 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.

127.0.0.1 example.com
127.0.0.1 examplealias.com

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

Tests

To run the tests:

python runtests.py

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-multisite2-2.0.1.tar.gz (88.6 kB view details)

Uploaded Source

Built Distribution

django_multisite2-2.0.1-py3-none-any.whl (120.5 kB view details)

Uploaded Python 3

File details

Details for the file django-multisite2-2.0.1.tar.gz.

File metadata

  • Download URL: django-multisite2-2.0.1.tar.gz
  • Upload date:
  • Size: 88.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for django-multisite2-2.0.1.tar.gz
Algorithm Hash digest
SHA256 bae603a76baf7035e776ae3c8289c79ea1336881694b96690f2745e94388c3ae
MD5 5e8f00365a8457f2cf0bd04a89302d5b
BLAKE2b-256 ecbb46b4a8e4e887557ec03e5ae864ba334052043d91d725888e3857a0640f02

See more details on using hashes here.

File details

Details for the file django_multisite2-2.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_multisite2-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 633cc8d95e51205c998ada6eea6a84037800245c614f8a5c0a55d5b95c0ae6f5
MD5 c726f428a4b61495015763b5bd48a705
BLAKE2b-256 d0db6396e80d37259e25567216cc26a5952b0d21227b4abff72f68803d794648

See more details on using hashes here.

Supported by

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