Serve multiple sites from a single Django application
Project description
README
Install with pip:
pip install django-multisite2
Quickstart
Replace your SITE_ID in settings.py to:
from multisite import SiteID SITE_ID = SiteID(default=1)
Add these to your INSTALLED_APPS:
INSTALLED_APPS = [ ... 'django.contrib.sites', 'multisite', ... ]
Add to your settings.py TEMPLATES loaders in the OPTIONS section:
TEMPLATES = [ ... { ... 'DIRS': {...} 'OPTIONS': { 'loaders': ( 'multisite.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', ) } ... } ... ]
Edit settings.py MIDDLEWARE:
MIDDLEWARE = ( ... 'multisite.middleware.DynamicSiteMiddleware', ... )
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 ... }, }
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.
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
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
Tests
To run the tests:
python runtests.py
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django-multisite-edc-2.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c1a986769f8dd815603941c101c7a950909ac1cf722f076ffe17b5f875f293b |
|
MD5 | ba4fdf2db05237f57d1aace0c7a6a69f |
|
BLAKE2b-256 | 7c5ecd1ca44d9363c7a6e6fbed4d9349e34e19f22e2ca85aae4c0208a3b7f8b8 |
Hashes for django_multisite_edc-2.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 72710b4628fcd2e3b0f87121ba88d397f57ae4c8b5de29aa1ac97da1a49fcdd4 |
|
MD5 | a8c92518687f8927412a60e1dd56170c |
|
BLAKE2b-256 | fcdc5a2f80396a9c179449e5ae0116af555783edd3e450c6af40248f39a19591 |