Skip to main content

Serve multiple sites from a single Django application

Project description

Get the code via git:

git clone git://github.com/ecometrica/django-multisite.git django-multisite

Add the django-multisite/multisite folder to your PYTHONPATH.

Replace your SITE_ID in settings.py to:

from multisite import SiteID
SITE_ID = SiteID()

Add to settings.py TEMPLATE_LOADERS:

TEMPLATE_LOADERS = (
    'multisite.template_loader.Loader',
    'django.template.loaders.app_directories.Loader',
)

Edit to settings.py MIDDLEWARE_CLASSES:

MIDDLEWARE_CLASSES = (
    ...
    '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.
# Default: '' (Empty string)
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
        ...
    },
}

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}

Create a directory settings.TEMPLATE_DIRS directory with the names of domains, such as:

mkdir templates/example.com

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-multisite-0.2.1.tar.gz (17.4 kB view details)

Uploaded Source

File details

Details for the file django-multisite-0.2.1.tar.gz.

File metadata

File hashes

Hashes for django-multisite-0.2.1.tar.gz
Algorithm Hash digest
SHA256 c6acd292294d5aae5b90c12861136c83342463576a8dd4b5197b69d8af5dde5f
MD5 4409548250c22abb5fc03907b4c0a628
BLAKE2b-256 580597f2ebe119a2a190f6ac2f779323df8652e6fee0b908fca3a8fad662c2d0

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