Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

66 lines (47 sloc) 2.046 kb
Django Dynamic Template Loader
There are times when you would like to use a different template based on the value of a HTTP request header. For example, different domain names, different browser types, or even the referring page. The dynamic template loader allows you to specify a separate list of directories in which Django should look for templates, based on a value in the request header.
1. Install ``dynamicloader`` in your Python path.
2. Add ``'dynamicloader.middleware.RequestMiddleware',`` into your ``MIDDLEWARE_CLASSES`` setting.
3. Add ``'dynamicloader.loader.load_template_source',`` at the *top* of your ``TEMPLATE_LOADERS`` setting.
4. Create a new setting, ``DYN_TEMPLATE_MAP``, in the following format::
re.compile('value1'): ('/path/to/directory','/path/to/directory2',),
re.compile('value2'): ('/path/to/directory3',)
re.compile('value1'): ('/path/to/directory4',)
Template Map Examples
Domain Name Differences
This example allows the override of certain templates based on the domain name or subdomain. In this setup, Django will look in ``/path/to/directory`` first if the ``HTTP_HOST`` includes the value ``www2``.
re.compile('www2'): ('/path/to/directory',),
Browser Differences
This example allows the delivery of different templates (or just specific ones) based on the user's browser.
re.compile('iPhone|iPod'): ('/path/to/directory',),
Query String Differences
This example checks the query string to decide which template to use based on a query string (the stuff after a '?' in a URL).
re.compile('imreallycool=1'): ('/path/to/directory',),
Jump to Line
Something went wrong with that request. Please try again.