I have templates in a dictionary and I’d like my template loader to fetch them. But since it’s instantiated by the Django code I can’t pass it the dictionary at that point.
What is the proper way to pass a variable to a template loader? (The dictionary is built dynamically, so I can’t use the Django settings file.)
My loader code, I can’t use dictionary yet:
class Loader(BaseLoader):
is_usable = True
def load_template_source(self, template_name, template_dirs=None):
if template_name in dictionary:
return (dictionary[template_name],template_name)
raise TemplateDoesNotExist("Could not find template '%s'." % template_name)
load_template_source.is_usable = True
What’s the problem? This should work.
Just define
dictionarysomewhere..If it needs to be dynamic, throw in a function that populates
dictionary.