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:

