Git Product home page Git Product logo

dynamicloader's Introduction

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.

Installing
==========

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::

	DYN_TEMPLATE_MAP = {
	    'HTTP_HEADER1': {
	        re.compile('value1'): ('/path/to/directory','/path/to/directory2',),
	        re.compile('value2'): ('/path/to/directory3',)
	    },
	    'HTTP_HEADER2': {
	        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``.
::

	DYN_TEMPLATE_MAP = {
	    'HTTP_HOST': {
	        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.
::

	DYN_TEMPLATE_MAP = {
	    'HTTP_USER_AGENT': {
	        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).
::

DYN_TEMPLATE_MAP = {
    'QUERY_STRING': {
        re.compile('imreallycool=1'): ('/path/to/directory',),
    },
}

dynamicloader's People

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.