Django dynamic template paths

Several add-on applications for Django bring in their own templates and expect user to hardcopy those files into project’s template directory. The problem raises when add-on egg is updated and its hardcopied templates are not. Another approach could be to add add-on template directory path to TEMPLATE_DIRS variable in project’s settings.py. However, once add-on application is updated, hardcoded path may not be up-to-date.

UPDATE: Vinicius Mendes commented on using django.template.loaders.app_directories.Loader to solve the problem (a similar module exists for eggs, which is named django.template.loaders.eggs.Loader). It’s definitely the easiest way if you don’t mind importing all of your applications templates. Instead, if you need to pick only selected directories or non-standard directory names, keep on reading.

The solution is to add a dynamic path that is resolved at runtime using add-on egg’s path. Here’s an example of how I got django-reversion templates added:

import os
from  pyclbr import readmodule_ex
 
TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(__file__), "../templates"),
    os.path.join(os.path.dirname(readmodule_ex('reversion')['__path__'][0]), "templates"),
)

Related posts:

  1. Integrate Wordpress and Django
  2. 8 web design add-ons for Django
  3. Integrate Tornado in Django
Add to StumbleUpon Add to del.icio.us Add to Mixx!     
blog comments powered by Disqus