How to change the template path dynamically in Django 1.8 -
i want add mobile version site, need change template path dynamically mobile template dir or desktop dir.
as template_dirs
deprecated since version 1.8, tried change dirs
option of djangotemplates
backend, didn't work.
as had same challenge post code here. using get_flavor django_mobile detect show , wanted use standard admin directories (as installed) django_admin_bootstrapped , regular django admin. in addition wanted interfere loader admin pages , not regular pages - in latter case loader nothing.
so works me:
import os.path django.template.loaders.base import loader django.template.loader import loaderorigin django.template import templatedoesnotexist django_mobile import get_flavour class myloader(loader): is_usable = true #this line necessary make work def load_template_source(self, template_name, template_dirs=none): path_split = template_name.split("/") if not u'admin' in path_split: raise templatedoesnotexist template_short_name = path_split[-1] if get_flavour() == "mobile": basepath = r'/path/to/django_admin_bootstrapped/templates/admin' path = os.path.join(basepath,template_short_name) else: basepath = r'/path/to/django/contrib/admin/templates/admin' path = os.path.join(basepath,template_short_name) try: open(path,"r") f1: template_string = f1.read() except ioerror: raise templatedoesnotexist template_origin = loaderorigin(template_short_name, self.load_template_source, template_name, template_dirs) return (template_string, template_origin)
if want distinguish template path different e.g. name in url need replace "if get_flavour()=="mobile" " looking inntemplate_name.
Comments
Post a Comment