diff options
| author | Aymeric Augustin <aymeric.augustin@m4x.org> | 2015-01-08 15:03:43 +0100 |
|---|---|---|
| committer | Aymeric Augustin <aymeric.augustin@m4x.org> | 2015-01-12 21:01:34 +0100 |
| commit | a3e783fe11dd25bbf84bfb6201186566ed473506 (patch) | |
| tree | 1fe6a54ab582e54f6b1f065f2236bfe2185ce254 /django | |
| parent | 71b7668b75d10589bbbdc7c5ca9ee7a125f91c90 (diff) | |
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.
Diffstat (limited to 'django')
| -rw-r--r-- | django/contrib/admin/templatetags/admin_list.py | 6 | ||||
| -rw-r--r-- | django/contrib/flatpages/views.py | 11 | ||||
| -rw-r--r-- | django/contrib/syndication/views.py | 6 | ||||
| -rw-r--r-- | django/template/backends/django.py | 32 | ||||
| -rw-r--r-- | django/template/response.py | 5 | ||||
| -rw-r--r-- | django/views/defaults.py | 15 |
6 files changed, 53 insertions, 22 deletions
diff --git a/django/contrib/admin/templatetags/admin_list.py b/django/contrib/admin/templatetags/admin_list.py index 266b880d99..6f7c530aa7 100644 --- a/django/contrib/admin/templatetags/admin_list.py +++ b/django/contrib/admin/templatetags/admin_list.py @@ -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) diff --git a/django/contrib/flatpages/views.py b/django/contrib/flatpages/views.py index 81d04c33b8..b8f65e4e55 100644 --- a/django/contrib/flatpages/views.py +++ b/django/contrib/flatpages/views.py @@ -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 diff --git a/django/contrib/syndication/views.py b/django/contrib/syndication/views.py index ea3777153b..da38323b01 100644 --- a/django/contrib/syndication/views.py +++ b/django/contrib/syndication/views.py @@ -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( diff --git a/django/template/backends/django.py b/django/template/backends/django.py index 63747f7c7f..2fe3227237 100644 --- a/django/template/backends/django.py +++ b/django/template/backends/django.py @@ -1,9 +1,12 @@ # 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: diff --git a/django/template/response.py b/django/template/response.py index 6f23f66b7d..8f175b14e6 100644 --- a/django/template/response.py +++ b/django/template/response.py @@ -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 diff --git a/django/views/defaults.py b/django/views/defaults.py index c33284e756..e71756d1dd 100644 --- a/django/views/defaults.py +++ b/django/views/defaults.py @@ -1,6 +1,5 @@ from django import http -from django.template import (Context, RequestContext, - loader, Template, TemplateDoesNotExist) +from django.template import loader, Context, Engine, TemplateDoesNotExist from django.views.decorators.csrf import requires_csrf_token @@ -17,15 +16,17 @@ def page_not_found(request, template_name='404.html'): request_path The path of the requested URL (e.g., '/app/pages/bad_page/') """ + context = {'request_path': request.path} try: template = loader.get_template(template_name) + body = template.render(context, request) content_type = None # Django will use DEFAULT_CONTENT_TYPE except TemplateDoesNotExist: - template = Template( + template = Engine().from_string( '<h1>Not Found</h1>' '<p>The requested URL {{ request_path }} was not found on this server.</p>') + body = template.render(Context(context)) content_type = 'text/html' - body = template.render(RequestContext(request, {'request_path': request.path})) return http.HttpResponseNotFound(body, content_type=content_type) @@ -41,7 +42,7 @@ def server_error(request, template_name='500.html'): template = loader.get_template(template_name) except TemplateDoesNotExist: return http.HttpResponseServerError('<h1>Server Error (500)</h1>', content_type='text/html') - return http.HttpResponseServerError(template.render(Context({}))) + return http.HttpResponseServerError(template.render()) @requires_csrf_token @@ -56,7 +57,7 @@ def bad_request(request, template_name='400.html'): template = loader.get_template(template_name) except TemplateDoesNotExist: return http.HttpResponseBadRequest('<h1>Bad Request (400)</h1>', content_type='text/html') - return http.HttpResponseBadRequest(template.render(Context({}))) + return http.HttpResponseBadRequest(template.render()) # This can be called when CsrfViewMiddleware.process_view has not run, @@ -77,4 +78,4 @@ def permission_denied(request, template_name='403.html'): template = loader.get_template(template_name) except TemplateDoesNotExist: return http.HttpResponseForbidden('<h1>403 Forbidden</h1>', content_type='text/html') - return http.HttpResponseForbidden(template.render(RequestContext(request))) + return http.HttpResponseForbidden(template.render(request=request)) |
