Commit 2f0566fa authored by Berker Peksag's avatar Berker Peksag Committed by Tim Graham
Browse files

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.
parent 89319850
Loading
Loading
Loading
Loading
+8 −7
Original line number Diff line number Diff line
@@ -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])
+16 −2
Original line number Diff line number Diff line
@@ -594,17 +594,31 @@ The Python API

``django.template.loader`` has two functions to load templates from files:

.. function:: get_template(template_name)
.. function:: get_template(template_name[, dirs])

    ``get_template`` returns the compiled template (a ``Template`` object) for
    the template with the given name. If the template doesn't exist, it raises
    ``django.template.TemplateDoesNotExist``.

.. function:: select_template(template_name_list)
    To override the :setting:`TEMPLATE_DIRS` setting, use the ``dirs``
    parameter. The ``dirs`` parameter may be a tuple or list.

    .. versionchanged:: 1.7

       The ``dirs`` parameter was added.

.. function:: select_template(template_name_list[, dirs])

    ``select_template`` is just like ``get_template``, except it takes a list
    of template names. Of the list, it returns the first template that exists.

    To override the :setting:`TEMPLATE_DIRS` setting, use the ``dirs``
    parameter. The ``dirs`` parameter may be a tuple or list.

    .. versionchanged:: 1.7

       The ``dirs`` parameter was added.

For example, if you call ``get_template('story_detail.html')`` and have the
above :setting:`TEMPLATE_DIRS` setting, here are the files Django will look for,
in order:
+8 −0
Original line number Diff line number Diff line
@@ -285,6 +285,14 @@ Templates
* ``TypeError`` exceptions are not longer silenced when raised during the
  rendering of a template.

* The following functions now accept a ``dirs`` parameter which is a list or
  tuple to override :setting:`TEMPLATE_DIRS`:

  * :func:`django.template.loader.get_template()`
  * :func:`django.template.loader.select_template()`
  * :func:`django.shortcuts.render()`
  * :func:`django.shortcuts.render_to_response()`

Tests
^^^^^

+33 −2
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@ introduce controlled coupling for convenience's sake.
``render``
==========

.. function:: render(request, template_name[, dictionary][, context_instance][, content_type][, status][, current_app])
.. function:: render(request, template_name[, dictionary][, context_instance][, content_type][, status][, current_app][, dirs])

   Combines a given template with a given context dictionary and returns an
   :class:`~django.http.HttpResponse` object with that rendered text.
@@ -58,6 +58,13 @@ Optional arguments
    :ref:`namespaced URL resolution strategy <topics-http-reversing-url-namespaces>`
    for more information.

``dirs``
    A tuple or list of values to override the :setting:`TEMPLATE_DIRS` setting.

.. versionchanged:: 1.7

   The ``dirs`` parameter was added.

Example
-------

@@ -83,11 +90,19 @@ This example is equivalent to::
        return HttpResponse(t.render(c),
            content_type="application/xhtml+xml")

If you want to override the :setting:`TEMPLATE_DIRS` setting, use the
``dirs`` parameter::

    from django.shortcuts import render

    def my_view(request):
        # View code here...
        return render(request, 'index.html', dirs=('custom_templates',))

``render_to_response``
======================

.. function:: render_to_response(template_name[, dictionary][, context_instance][, content_type])
.. function:: render_to_response(template_name[, dictionary][, context_instance][, content_type][, dirs])

   Renders a given template with a given context dictionary and returns an
   :class:`~django.http.HttpResponse` object with that rendered text.
@@ -125,6 +140,13 @@ Optional arguments
    The MIME type to use for the resulting document. Defaults to the value of
    the :setting:`DEFAULT_CONTENT_TYPE` setting.

``dirs``
    A tuple or list of values to override the :setting:`TEMPLATE_DIRS` setting.

.. versionchanged:: 1.7

   The ``dirs`` parameter was added.

Example
-------

@@ -150,6 +172,15 @@ This example is equivalent to::
        return HttpResponse(t.render(c),
            content_type="application/xhtml+xml")

If you want to override the :setting:`TEMPLATE_DIRS` setting, use the
``dirs`` parameter::

    from django.shortcuts import render_to_response

    def my_view(request):
        # View code here...
        return render_to_response('index.html', dirs=('custom_templates',))

``redirect``
============

+1 −0
Original line number Diff line number Diff line
spam eggs{{ obj }}
Loading