diff options
| author | Berker Peksag <berker.peksag@gmail.com> | 2013-09-16 00:51:24 +0300 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2013-09-18 07:37:08 -0400 |
| commit | 2f0566fa61e13277364e3aef338fa5c143f5a704 (patch) | |
| tree | 207438ec3b7983be24dc972890dda923a9f4ee45 /django | |
| parent | 893198509e3b821a56a56e4326929e0613aad983 (diff) | |
Fixed #4278 -- Added a dirs parameter to a few functions to override TEMPLATE_DIRS.
* django.template.loader.get_template()
* django.template.loader.select_template()
* django.shortcuts.render()
* django.shortcuts.render_to_response()
Thanks amcnabb for the suggestion.
Diffstat (limited to 'django')
| -rw-r--r-- | django/template/loader.py | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/django/template/loader.py b/django/template/loader.py index 30d3271960..410b03726a 100644 --- a/django/template/loader.py +++ b/django/template/loader.py @@ -130,12 +130,12 @@ def find_template(name, dirs=None): pass raise TemplateDoesNotExist(name) -def get_template(template_name): +def get_template(template_name, dirs=None): """ Returns a compiled Template object for the given template name, handling template inheritance recursively. """ - template, origin = find_template(template_name) + template, origin = find_template(template_name, dirs) if not hasattr(template, 'render'): # template needs to be compiled template = get_template_from_string(template, origin, template_name) @@ -148,7 +148,8 @@ def get_template_from_string(source, origin=None, name=None): """ return Template(source, origin, name) -def render_to_string(template_name, dictionary=None, context_instance=None): +def render_to_string(template_name, dictionary=None, context_instance=None, + dirs=None): """ Loads the given template_name and renders it with the given dictionary as context. The template_name may be a string to load a single template using @@ -157,9 +158,9 @@ def render_to_string(template_name, dictionary=None, context_instance=None): """ dictionary = dictionary or {} if isinstance(template_name, (list, tuple)): - t = select_template(template_name) + t = select_template(template_name, dirs) else: - t = get_template(template_name) + t = get_template(template_name, dirs) if not context_instance: return t.render(Context(dictionary)) # Add the dictionary to the context stack, ensuring it gets removed again @@ -167,14 +168,14 @@ def render_to_string(template_name, dictionary=None, context_instance=None): with context_instance.push(dictionary): return t.render(context_instance) -def select_template(template_name_list): +def select_template(template_name_list, dirs=None): "Given a list of template names, returns the first that can be loaded." if not template_name_list: raise TemplateDoesNotExist("No template names provided") not_found = [] for template_name in template_name_list: try: - return get_template(template_name) + return get_template(template_name, dirs) except TemplateDoesNotExist as e: if e.args[0] not in not_found: not_found.append(e.args[0]) |
