Commit 90236966 authored by Tim Graham's avatar Tim Graham
Browse files

Removed dictionary and context_instance parameters for render functions.

Per deprecation timeline.
parent b3641512
Loading
Loading
Loading
Loading
+5 −41
Original line number Diff line number Diff line
@@ -10,63 +10,27 @@ from django.db.models.query import QuerySet
from django.http import (
    Http404, HttpResponse, HttpResponsePermanentRedirect, HttpResponseRedirect,
)
from django.template import RequestContext, loader
from django.template.engine import (
    _context_instance_undefined, _dictionary_undefined
)
from django.template import loader
from django.utils import six
from django.utils.encoding import force_text
from django.utils.functional import Promise


def render_to_response(template_name, context=None,
                       context_instance=_context_instance_undefined,
                       content_type=None, status=None,
                       dictionary=_dictionary_undefined, using=None):
def render_to_response(template_name, context=None, content_type=None, status=None, using=None):
    """
    Returns a HttpResponse whose content is filled with the result of calling
    django.template.loader.render_to_string() with the passed arguments.
    """
    if (context_instance is _context_instance_undefined
            and dictionary is _dictionary_undefined):
        # No deprecated arguments were passed - use the new code path
    content = loader.render_to_string(template_name, context, using=using)

    else:
        # Some deprecated arguments were passed - use the legacy code path
        content = loader.render_to_string(
            template_name, context, context_instance, dictionary,
            using=using)

    return HttpResponse(content, content_type, status)


def render(request, template_name, context=None,
           context_instance=_context_instance_undefined,
           content_type=None, status=None,
           dictionary=_dictionary_undefined,
           using=None):
def render(request, template_name, context=None, content_type=None, status=None, using=None):
    """
    Returns a HttpResponse whose content is filled with the result of calling
    django.template.loader.render_to_string() with the passed arguments.
    Uses a RequestContext by default.
    """
    if (context_instance is _context_instance_undefined
            and dictionary is _dictionary_undefined):
        # No deprecated arguments were passed - use the new code path
        # In Django 1.10, request should become a positional argument.
        content = loader.render_to_string(
            template_name, context, request=request, using=using)
    else:
        # Some deprecated arguments were passed - use the legacy code path
        if context_instance is not _context_instance_undefined:
            pass
        else:
            context_instance = RequestContext(request)
        content = loader.render_to_string(
            template_name, context, context_instance, dictionary,
            using=using)

    """
    content = loader.render_to_string(template_name, context, request, using=using)
    return HttpResponse(content, content_type, status)


+7 −33
Original line number Diff line number Diff line
@@ -11,9 +11,6 @@ from .context import _builtin_context_processors
from .exceptions import TemplateDoesNotExist
from .library import import_library

_context_instance_undefined = object()
_dictionary_undefined = object()


class Engine(object):
    default_builtins = [
@@ -183,40 +180,17 @@ class Engine(object):
    # will be removed in Django 1.10. It's superseded by a new render_to_string
    # function in django.template.loader.

    def render_to_string(self, template_name, context=None,
                         context_instance=_context_instance_undefined,
                         dictionary=_dictionary_undefined):
        if context_instance is _context_instance_undefined:
            context_instance = None
        else:
            warnings.warn(
                "The context_instance argument of render_to_string is "
                "deprecated.", RemovedInDjango110Warning, stacklevel=2)
        if dictionary is _dictionary_undefined:
            dictionary = None
        else:
            warnings.warn(
                "The dictionary argument of render_to_string was renamed to "
                "context.", RemovedInDjango110Warning, stacklevel=2)
            context = dictionary

    def render_to_string(self, template_name, context=None):
        if isinstance(template_name, (list, tuple)):
            t = self.select_template(template_name)
        else:
            t = self.get_template(template_name)
        if not context_instance:
        # Django < 1.8 accepted a Context in `context` even though that's
        # unintended. Preserve this ability but don't rewrap `context`.
        if isinstance(context, Context):
            return t.render(context)
        else:
            return t.render(Context(context))
        if not context:
            return t.render(context_instance)
        # Add the context to the context stack, ensuring it gets removed again
        # to keep the context_instance in the same state it started in.
        with context_instance.push(context):
            return t.render(context_instance)

    def select_template(self, template_name_list):
        """
+5 −50
Original line number Diff line number Diff line
@@ -3,8 +3,6 @@ import warnings
from django.utils.deprecation import RemovedInDjango110Warning

from . import engines
from .backends.django import DjangoTemplates
from .engine import _context_instance_undefined, _dictionary_undefined
from .exceptions import TemplateDoesNotExist
from .loaders import base

@@ -49,61 +47,18 @@ def select_template(template_name_list, using=None):
        raise TemplateDoesNotExist("No template names provided")


def render_to_string(template_name, context=None,
                     context_instance=_context_instance_undefined,
                     dictionary=_dictionary_undefined,
                     request=None, using=None):
def render_to_string(template_name, context=None, request=None, using=None):
    """
    Loads a template and renders it with a context. Returns a string.

    template_name may be a string or a list of strings.
    """
    if (context_instance is _context_instance_undefined
            and dictionary is _dictionary_undefined):
        # No deprecated arguments were passed - use the new code path
    if isinstance(template_name, (list, tuple)):
        template = select_template(template_name, using=using)
    else:
        template = get_template(template_name, using=using)
    return template.render(context, request)

    else:
        chain = []
        # Some deprecated arguments were passed - use the legacy code path
        for engine in _engine_list(using):
            try:
                # This is required for deprecating properly arguments specific
                # to Django templates. Remove Engine.render_to_string() at the
                # same time as this code path in Django 1.10.
                if isinstance(engine, DjangoTemplates):
                    if request is not None:
                        raise ValueError(
                            "render_to_string doesn't support the request argument "
                            "when some deprecated arguments are passed.")
                    # Hack -- use the internal Engine instance of DjangoTemplates.
                    return engine.engine.render_to_string(
                        template_name, context, context_instance, dictionary)
                elif context_instance is not _context_instance_undefined:
                    warnings.warn(
                        "Skipping template backend %s because its render_to_string "
                        "method doesn't support the context_instance argument." %
                        engine.name, stacklevel=2)
                elif dictionary is not _dictionary_undefined:
                    warnings.warn(
                        "Skipping template backend %s because its render_to_string "
                        "method doesn't support the dictionary argument." %
                        engine.name, stacklevel=2)
            except TemplateDoesNotExist as e:
                chain.append(e)
                continue

        if template_name:
            if isinstance(template_name, (list, tuple)):
                template_name = ', '.join(template_name)
            raise TemplateDoesNotExist(template_name, chain=chain)
        else:
            raise TemplateDoesNotExist("No template names provided")


def _engine_list(using=None):
    return engines.all() if using is None else [engines[using]]
+4 −40
Original line number Diff line number Diff line
@@ -15,14 +15,13 @@ introduce controlled coupling for convenience's sake.
``render``
==========

.. function:: render(request, template_name, context=None, context_instance=_context_instance_undefined, content_type=None, status=None, using=None)
.. function:: render(request, template_name, context=None, content_type=None, status=None, using=None)

   Combines a given template with a given context dictionary and returns an
   :class:`~django.http.HttpResponse` object with that rendered text.

   :func:`render()` is the same as a call to
   :func:`render_to_response()` with a ``context_instance`` argument that
   forces the use of a :class:`~django.template.RequestContext`.
   :func:`render()` is the same as a call to :func:`render_to_response()` but
   it also makes the current request available in the template.

   Django does not provide a shortcut function which returns a
   :class:`~django.template.response.TemplateResponse` because the constructor
@@ -46,20 +45,6 @@ Optional arguments
    is an empty dictionary. If a value in the dictionary is callable, the
    view will call it just before rendering the template.

    .. versionchanged:: 1.8

       The ``context`` argument used to be called ``dictionary``. That name
       is deprecated in Django 1.8 and will be removed in Django 1.10.

``context_instance``
    The context instance to render the template with. By default, the template
    will be rendered with a ``RequestContext`` instance (filled with values from
    ``request`` and ``context``).

    .. deprecated:: 1.8

       The ``context_instance`` argument is deprecated. Simply use ``context``.

``content_type``
    The MIME type to use for the resulting document. Defaults to the value of
    the :setting:`DEFAULT_CONTENT_TYPE` setting.
@@ -103,7 +88,7 @@ This example is equivalent to::
``render_to_response``
======================

.. function:: render_to_response(template_name, context=None, context_instance=_context_instance_undefined, content_type=None, status=None, using=None)
.. function:: render_to_response(template_name, context=None, content_type=None, status=None, using=None)

   Renders a given template with a given context dictionary and returns an
   :class:`~django.http.HttpResponse` object with that rendered text.
@@ -125,27 +110,6 @@ Optional arguments
    is an empty dictionary. If a value in the dictionary is callable, the
    view will call it just before rendering the template.

    .. versionchanged:: 1.8

       The ``context`` argument used to be called ``dictionary``. That name
       is deprecated in Django 1.8 and will be removed in Django 1.10.

``context_instance``
    The context instance to render the template with. By default, the template
    will be rendered with a :class:`~django.template.Context` instance (filled
    with values from ``context``). If you need to use :ref:`context
    processors <subclassing-context-requestcontext>`, render the template with
    a :class:`~django.template.RequestContext` instance instead. Your code
    might look something like this::

        return render_to_response('my_template.html',
                                  my_context,
                                  context_instance=RequestContext(request))

    .. deprecated:: 1.8

       The ``context_instance`` argument is deprecated. Simply use ``context``.

``content_type``
    The MIME type to use for the resulting document. Defaults to the value of
    the :setting:`DEFAULT_CONTENT_TYPE` setting.
+1 −19
Original line number Diff line number Diff line
@@ -261,7 +261,7 @@ the following templates:
In addition, to cut down on the repetitive nature of loading and rendering
templates, Django provides a shortcut function which automates the process.

.. function:: render_to_string(template_name, context=None, context_instance=_context_instance_undefined, request=None, using=None)
.. function:: render_to_string(template_name, context=None, request=None, using=None)

    ``render_to_string()`` loads a template like :func:`get_template` and
    calls its ``render()`` method immediately. It takes the following
@@ -275,24 +275,6 @@ templates, Django provides a shortcut function which automates the process.
    ``context``
        A :class:`dict` to be used as the template's context for rendering.

        .. versionchanged:: 1.8

            The ``context`` argument used to be called ``dictionary``. That name
            is deprecated in Django 1.8 and will be removed in Django 1.10.

            ``context`` is now optional. An empty context will be used if it
            isn't provided.

    ``context_instance``
        An instance of :class:`~django.template.Context` or a subclass (e.g., an
        instance of :class:`~django.template.RequestContext`) to use as the
        template's context.

        .. deprecated:: 1.8

            The ``context_instance`` argument is deprecated. Use ``context`` and
            if needed ``request``.

    ``request``
        An optional :class:`~django.http.HttpRequest` that will be available
        during the template's rendering process.
Loading