Commit a3e783fe authored by Aymeric Augustin's avatar Aymeric Augustin
Browse files

Deprecated passing a Context to a generic Template.render.

A deprecation path is required because the return type of
django.template.loader.get_template changed during the
multiple template engines refactor.

test_csrf_token_in_404 was incorrect: it tested the case when the
hardcoded template was rendered, and that template doesn't depend on the
CSRF token. This commit makes it test the case when a custom template is
rendered.
parent 71b7668b
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -19,7 +19,7 @@ from django.utils.translation import ugettext as _
from django.utils.encoding import force_text
from django.template import Library
from django.template.loader import get_template
from django.template.context import Context


register = Library()

@@ -412,11 +412,11 @@ def search_form(cl):
@register.simple_tag
def admin_list_filter(cl, spec):
    tpl = get_template(spec.template)
    return tpl.render(Context({
    return tpl.render({
        'title': spec.title,
        'choices': list(spec.choices(cl)),
        'spec': spec,
    }))
    })


@register.inclusion_tag('admin/actions.html', takes_context=True)
+4 −7
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@ from django.contrib.flatpages.models import FlatPage
from django.contrib.sites.shortcuts import get_current_site
from django.http import Http404, HttpResponse, HttpResponsePermanentRedirect
from django.shortcuts import get_object_or_404
from django.template import loader, RequestContext
from django.template import loader
from django.utils.safestring import mark_safe
from django.views.decorators.csrf import csrf_protect

@@ -58,9 +58,9 @@ def render_flatpage(request, f):
        from django.contrib.auth.views import redirect_to_login
        return redirect_to_login(request.path)
    if f.template_name:
        t = loader.select_template((f.template_name, DEFAULT_TEMPLATE))
        template = loader.select_template((f.template_name, DEFAULT_TEMPLATE))
    else:
        t = loader.get_template(DEFAULT_TEMPLATE)
        template = loader.get_template(DEFAULT_TEMPLATE)

    # To avoid having to always use the "|safe" filter in flatpage templates,
    # mark the title and content as already safe (since they are raw HTML
@@ -68,8 +68,5 @@ def render_flatpage(request, f):
    f.title = mark_safe(f.title)
    f.content = mark_safe(f.content)

    c = RequestContext(request, {
        'flatpage': f,
    })
    response = HttpResponse(t.render(c))
    response = HttpResponse(template.render({'flatpage': f}, request))
    return response
+3 −3
Original line number Diff line number Diff line
@@ -6,7 +6,7 @@ from django.conf import settings
from django.contrib.sites.shortcuts import get_current_site
from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
from django.http import HttpResponse, Http404
from django.template import loader, TemplateDoesNotExist, RequestContext
from django.template import loader, TemplateDoesNotExist
from django.utils import feedgenerator
from django.utils.encoding import force_text, iri_to_uri, smart_text
from django.utils.html import escape
@@ -162,11 +162,11 @@ class Feed(object):
            context = self.get_context_data(item=item, site=current_site,
                                            obj=obj, request=request)
            if title_tmp is not None:
                title = title_tmp.render(RequestContext(request, context))
                title = title_tmp.render(context, request)
            else:
                title = self.__get_dynamic_attr('item_title', item)
            if description_tmp is not None:
                description = description_tmp.render(RequestContext(request, context))
                description = description_tmp.render(context, request)
            else:
                description = self.__get_dynamic_attr('item_description', item)
            link = add_domain(
+30 −2
Original line number Diff line number Diff line
# Since this package contains a "django" module, this is required on Python 2.
from __future__ import absolute_import

import warnings

from django.conf import settings
from django.template.context import Context, RequestContext
from django.template.engine import _dirs_undefined, Engine
from django.utils.deprecation import RemovedInDjango20Warning


from .base import BaseEngine
@@ -40,8 +43,33 @@ class Template(object):
        return self.template.origin

    def render(self, context=None, request=None):
        # TODO: require context to be a dict -- through a deprecation path?
        if not isinstance(context, Context):
        # A deprecation path is required here to cover the following usage:
        # >>> from django.template import Context
        # >>> from django.template.loader import get_template
        # >>> template = get_template('hello.html')
        # >>> template.render(Context({'name': 'world'}))
        # In Django 1.7 get_template() returned a django.template.Template.
        # In Django 1.8 it returns a django.template.backends.django.Template.
        # In Django 2.0 the isinstance checks should be removed. If passing a
        # Context or a RequestContext works by accident, it won't be an issue
        # per se, but it won't be officially supported either.
        if isinstance(context, RequestContext):
            if request is not None and request is not context.request:
                raise ValueError(
                    "render() was called with a RequestContext and a request "
                    "argument which refer to different requests. Make sure "
                    "that the context argument is a dict or at least that "
                    "the two arguments refer to the same request.")
            warnings.warn(
                "render() must be called with a dict, not a RequestContext.",
                RemovedInDjango20Warning, stacklevel=2)

        elif isinstance(context, Context):
            warnings.warn(
                "render() must be called with a dict, not a Context.",
                RemovedInDjango20Warning, stacklevel=2)

        else:
            if request is None:
                context = Context(context)
            else:
+5 −0
Original line number Diff line number Diff line
@@ -82,6 +82,11 @@ class SimpleTemplateResponse(HttpResponse):
        """
        template = self.resolve_template(self.template_name)
        context = self.resolve_context(self.context_data)
        # TODO - remove this hack - makes the tests pass until the next commit
        try:
            template = template.template
        except AttributeError:
            pass
        content = template.render(context)
        return content

Loading