1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

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.
This commit is contained in:
Berker Peksag
2013-09-16 00:51:24 +03:00
committed by Tim Graham
parent 893198509e
commit 2f0566fa61
10 changed files with 111 additions and 12 deletions

View File

@@ -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])