Switching Django templates

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

I need to create a set of templates for the mobile version and would like the templates to sit inside it and not inside the root dir template i.e. I would like to see two templates template templates and "mobile_templates" instead of having to use "templates / mobile" for the latter.

Do I need to write my own template loader?

+2


source to share


1 answer


You can install multiple template directories in your settings file, and Django will search for them in the order in which you list them. The problem is you don't care if you want template_x.html from directory a or b. If you have the same pattern_x in directory a and b, it will pull from which the first one was ever specified, which can be confusing. A good way would be this:

There is only one template directory somewhere called "templates". Inside this folder, there is a folder called "mobile" and a template called "default" (or whatever). Then when you call your template you just need to use the directory path.

In your opinion:

# 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' %}

      

Regular pattern:

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

      

Settings file:

TEMPLATE_DIRS = (
    'D:/some/path/to/templates',
)

      

+2


source







All Articles