Django theme (template) changer app
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


documentation at: readTheDocs


Django Chameleon is a dinamic theme (template) changer for the Django projects.


  • Django >= 1.2 [if we use the loader method]
  • Django >= 1.3 [if we use the TemplateResponse method]
  • Python >= 2.4 (I think, I use 2.6)
  • Session middleware (I think that is necessary to save the satatus of the theme)
  • RequestContext [OPTIONAL] but recommended

How it works

Chameleon gets the theme from the POST or GET request, then inserts in the session and the request context. You can set and get the theme in the cookie with some methods that are in chameleon.utils

Chameleon supports the theme change in two ways. With a loader or using the new TemplateResponse instead of HttpResponse (render_to_response...) this has been added in Django 1.3. For example a view that uses this new feature (doesn't change the previous functionality) only manages the response in a different way that the middleware can interact with the response before rendering. This is an example

#from django.shortcuts import render_to_response
#from django.template import RequestContext
from django.template.response import TemplateResponse
from forms import ColorForm

def index(request):

    #return render_to_response('index.html', context_instance=RequestContext(request))
    return TemplateResponse(request, 'index.html', {'form': ColorForm()})

In my opinion I recomend using TemplateResponse to not mess with the default Django loaders, or any other custom loaders.

###Settings template structure###


For example we have a basic structure inside templates (the root where django will search for the templates). In the root there are base.html and index.html. index.html extends from base.html and in the views the template to render is index.html. So we add other themes, red, blue, green and black. These themes the only requeriment that need is to have is index.html because is the one that is rendered from the view. If all the themes want to extend from the same base.html they could. Also they could extend from an individual base.html but this has to be put in the template, for example in the blue one the extend from /templates/blue/base.html is: {% extends 'blue/base.html' %}

As we can see all the templates have the same structure. default is in templates, blue and red are in templates/xxxx, and green and black are templates/themes/xxxx but always the same structure of templates

├── base.html
├── index.html
├── red
│   ├── [base.html] 
│   └── index.html
├── blue
│   ├── [base.html] 
│   └── index.html
└── themes
    ├── black
    │   ├── [base.html] 
    │   └── index.html
    └── green
        ├── [base.html] 
        └── index.html

###Settings variables###

There are some vars that can be set in TEMPLATE_CONTEXT_PROCESSOR and CHAMELEON_SITE_THEMES are neccesary, the others are optional. For example:

from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS

TEMPLATE_CONTEXT_PROCESSORS += ('chameleon.context_processors.theme',)

# Django settings for chameleonTest project.

# [OPTIONAL] If you want to manage the theme change looking in your templates  manually 
# with the request context var. Put down this flag

# [OPTIONAL] If you want to name the form var in a different name

# [OPTIONAL] If you want to name the default theme different to 'default'

# [OPTIONAL] If you want to change the var name in the context, set this

# [OPTIONAL] If you want to change the var name in the cookie set this

# The themes and their paths(the path where the structure starts). 
# The structure of the theme has to be the same as the default one.
# If a empty then the name of the theme will be the folder to search in  
    'blue': 'blue',

###Add chameleon to installed apps### In settings add chameleon to INSTALLED_APPS, for example:


###Add middleware### Add both middlewares to MIDDLEWARE_CLASSES (the second one is only for the TemplateResponse method, but if loader is activated in TEMPLATE_LOADERS the SetResponseTemplate middleware disables automatically)


###Select method. loader or TemplateResponse###

We have to select the method between Loader and TemplateResponse(Recommended)


Add our loader to settings (the first one) for example:



  • We don't have to add the loader to TEMPLATE_LOADERS
  • All the views have to call the template with TemplateResponse (or SimpleTemplateResponse)


If you want that the default option of the form (theme choice) renders the theme that currently is activated you have to use chameleon.utils methods like is in the example( The example shows how to use the app with a basic example

Current status

  • Initial state
  • Basic functionality
  • No installer
  • Not many tests
  • Many bugs :D


3 clause/New BSD license: opensource, wikipedia


Thanks to django-mobile for the inspiration and code learning :)