views:

215

answers:

1

Does anyone know a way to switch the TEMPLATE_DIR in django dynamically.

I need to create a set of templates for a mobile version and would like the templates to sit inside there own dir instead of inside the root template dir ie: I would like 2 template dirs 'templates' and 'mobile_templates' and not have to use 'templates/mobile' for the latter.

Do I have to write my own template loader?

+2  A: 

Hey SleepyJames,

You can set multiple template directories in your settings file and Django will search them in the order that you list them. Problem is that it doesn't care if you want template_x.html from directory a or b. If you have the same template_x in directory a and in b, it'll pull from which ever is listed first which can be confusing. A good way would be as follows:

Have only 1 template directory somewhere called 'templates'. Inside of this folder have a folder called 'mobile' and a template called 'default' (or whatever). Then when you call your template you just have to use the directory path as well.

In your view:

# some mobile view (everything omitted brevity)
get_template('mobile/some_template.html')

# some normal view (everything omitted brevity)
get_template('default/some_template.html')

In your templates:

Mobile Template:

{% extends 'mobile/base.html' %}

Normal Template:

{% extends 'default/base.html' %}

Settings File:

TEMPLATE_DIRS = (
    'D:/some/path/to/templates',
)
orokusaki
This is probably best. If your naming conventions are fine (like 'mobile_whatever.html'), you'll be fine.
Adam Nelson
I really wanted to have the template loader switch it's source dir based on certain criteria, but I guess this would be a more sensible approach.
sleepyjames