Simple multihost handling for Django
Project description
Overview
Django-Multihost is a Django middleware that overrides Site ORM functionality and gets the current Site from the Host header. The code is loosely based on the work done by Bruce Kroeze here: django-threaded-multihost.
Description
The MultiHostMiddleware class contained in this app has 2 tasks:
Upon middleware __init__(), it overrides SiteManager.get_current() with a multihost-aware version that reads the Host header from the request and attempts to find a matching Site (caches it if cache is enabled).
Upon middleware process_request(), it sets the request into threadlocal storage and then uses the new SiteManager.get_current() functionality to retrieve the Site matching the Host header and sets it into the request instance. If a match is not found and MULTIHOST_AUTO_WWW is True (the default), it will attempt to modify the Host URL to add/remove www. and attempt the lookup again. If it still doesn’t find a matching Site, it redirects to MULTIHOST_REDIRECT_URL.
Requirements
Python 2.6 (May work with 2.3+, but untested - please report)
Django 1.2.x (May work with 1.0+, but untested - please report)
Notes
This app has been built to be modular so that developers can easily replace middleware functionality through specific settings.py changes. See below for more information.
Installation
Copy or symlink the multihost package into your django project directory or install it by running one of the following commands:
python setup.py install
or
pip install django-multihost
or
easy_install django-multihost
Add MULTIHOST_REDIRECT_URL to settings.py
Add multihost.middleware.MultiHostMiddleware to your MIDDLEWARE_CLASSES in at the end of the list. If you find it isn’t working, move it up the list order as there may be a middleware conflict.
Advanced Settings
There are 4 settings that developers can use to override default functionality:
MULTIHOST_REDIRECT_URL: required; no default. The default middleware request processing will redirect to this URL in the case of Site lookup failure.
MULTIHOST_AUTO_WWW: optional; defaults to True. If the Site can’t be found that matches the incoming Host header exactly, this will automatically try removing/adding www. and searching again.
MULTIHOST_FUNC_INIT: optional. The default function overrides SiteManager.get_current() with a multihost-aware version.
MULTIHOST_FUNC_PROCESSREQUEST: optional. The default function sets the request into threadlocal storage and then attempts to look up the Site matching the Host header.
Source
The latest source code can always be found here: github.com/jaddison/django-multihost
Credits
Django-Multihost is maintained by James Addison.
License
Django-Multihost is Copyright (c) 2010, James Addison. It is free software, and may be redistributed under the terms specified in the LICENSE file.
Questions, Comments, Concerns:
Feel free to open an issue here: github.com/jaddison/django-multihost/issues
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
Built Distribution
Hashes for django-multihost-0.1.win-amd64.exe
Algorithm | Hash digest | |
---|---|---|
SHA256 | 080bd792b1827bb0d886a35d6f8b1768ab58c3ab4510b804843a4473e1db3197 |
|
MD5 | ce75e6b65272dcb33d7a102370761765 |
|
BLAKE2b-256 | 3f0ad6dd043c1232c93c51ac7a5660ac417f823272447d76d5892fecc3a5efdf |