October archive

Installing django-threaded-multihost

I'm interested in serving multiple sites from a single django installation. The sites will be exactly the same, though with different db-driven content, so it seems unnecessary to have multiple django settings files and therefore distinct apache configurations. This is usually called "multihost." An early implementation of this idea is django-multihost. django-multihost is good, but it doesn't use the sites framework which can break some of the contrib applications.

Another implementation is django-threaded-multihost, which been factored out of Satchmo. It does respect and use the sites framework, but it has no install documentation. But since it was in Satchmo, there's code using it which can be used to write our own, horray.

First, get the package and do the basic install:

hg clone http://bitbucket.org/bkroeze/django-threaded-multihost/ sudo python setup.py install

Next, add the middleware. The satchmo install docs have this as the last entry in MIDDLEWARE_CLASSES before the satchmo-specific middleware.

MIDDLEWARE_CLASSES = ( ... "threaded_multihost.middleware.ThreadLocalMiddleware", )

django-threaded-multihost has a custom implementation of get_current() for the Site manager. This is the component which allows a single django installation to "easily" distinguish between multiple sites, by freeing the django instance from reliance upon the settings.SITE_ID setting. In order to use this, the threaded_multihost.multihost_patch must be imported somewhere in your installation. The easiest place is probably in an __init__ file for the primary application on your site. So do:

from threaded_multihost import multihost_patch

And that's it. Any further requests to Sites.objects.get_current() will return the appropriate site object depending upon the request URL or fallback on settings.SITE_ID.

except that keyedcache doesn't work. So install keyedcache from bitbucket. then set the settings CACHE_PREFIX = 'whatever' and one oother one.