mirror of https://github.com/django/django.git
Removed support for passing a context to a generic Template.render().
Per deprecation timeline; refs a3e783fe11
.
This commit is contained in:
parent
4811f09fa3
commit
9114fe8ada
|
@ -2,18 +2,16 @@
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import warnings
|
|
||||||
from importlib import import_module
|
from importlib import import_module
|
||||||
from pkgutil import walk_packages
|
from pkgutil import walk_packages
|
||||||
|
|
||||||
from django.apps import apps
|
from django.apps import apps
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.template import TemplateDoesNotExist
|
from django.template import TemplateDoesNotExist
|
||||||
from django.template.context import Context, RequestContext, make_context
|
from django.template.context import make_context
|
||||||
from django.template.engine import Engine
|
from django.template.engine import Engine
|
||||||
from django.template.library import InvalidTemplateLibrary
|
from django.template.library import InvalidTemplateLibrary
|
||||||
from django.utils import six
|
from django.utils import six
|
||||||
from django.utils.deprecation import RemovedInDjango110Warning
|
|
||||||
|
|
||||||
from .base import BaseEngine
|
from .base import BaseEngine
|
||||||
|
|
||||||
|
@ -62,35 +60,7 @@ class Template(object):
|
||||||
return self.template.origin
|
return self.template.origin
|
||||||
|
|
||||||
def render(self, context=None, request=None):
|
def render(self, context=None, request=None):
|
||||||
# A deprecation path is required here to cover the following usage:
|
context = make_context(context, request)
|
||||||
# >>> 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 1.10 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.",
|
|
||||||
RemovedInDjango110Warning, stacklevel=2)
|
|
||||||
|
|
||||||
elif isinstance(context, Context):
|
|
||||||
warnings.warn(
|
|
||||||
"render() must be called with a dict, not a Context.",
|
|
||||||
RemovedInDjango110Warning, stacklevel=2)
|
|
||||||
|
|
||||||
else:
|
|
||||||
context = make_context(context, request)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return self.template.render(context)
|
return self.template.render(context)
|
||||||
except TemplateDoesNotExist as exc:
|
except TemplateDoesNotExist as exc:
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
from django.test import SimpleTestCase, ignore_warnings, override_settings
|
from django.test import SimpleTestCase, override_settings
|
||||||
from django.test.utils import require_jinja2
|
from django.test.utils import require_jinja2
|
||||||
from django.utils.deprecation import RemovedInDjango110Warning
|
|
||||||
|
|
||||||
|
|
||||||
@override_settings(
|
@override_settings(
|
||||||
|
@ -39,16 +38,6 @@ class ShortcutTests(SimpleTestCase):
|
||||||
response = self.client.get('/render_to_response/using/?using=jinja2')
|
response = self.client.get('/render_to_response/using/?using=jinja2')
|
||||||
self.assertEqual(response.content, b'Jinja2\n')
|
self.assertEqual(response.content, b'Jinja2\n')
|
||||||
|
|
||||||
@ignore_warnings(category=RemovedInDjango110Warning)
|
|
||||||
def test_render_to_response_with_context_instance_misuse(self):
|
|
||||||
"""
|
|
||||||
For backwards-compatibility, ensure that it's possible to pass a
|
|
||||||
RequestContext instance in the dictionary argument instead of the
|
|
||||||
context_instance argument.
|
|
||||||
"""
|
|
||||||
response = self.client.get('/render_to_response/context_instance_misuse/')
|
|
||||||
self.assertContains(response, 'context processor output')
|
|
||||||
|
|
||||||
def test_render(self):
|
def test_render(self):
|
||||||
response = self.client.get('/render/')
|
response = self.client.get('/render/')
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
from template_tests.test_response import test_processor_name
|
from template_tests.test_response import test_processor_name
|
||||||
|
|
||||||
from django.template import RequestContext
|
|
||||||
from django.template.backends.django import DjangoTemplates
|
from django.template.backends.django import DjangoTemplates
|
||||||
from django.template.library import InvalidTemplateLibrary
|
from django.template.library import InvalidTemplateLibrary
|
||||||
from django.test import RequestFactory, ignore_warnings, override_settings
|
from django.test import RequestFactory, override_settings
|
||||||
from django.utils.deprecation import RemovedInDjango110Warning
|
|
||||||
|
|
||||||
from .test_dummy import TemplateStringsTests
|
from .test_dummy import TemplateStringsTests
|
||||||
|
|
||||||
|
@ -36,23 +34,6 @@ class DjangoTemplatesTests(TemplateStringsTests):
|
||||||
content = template.render({'processors': 'no'}, request)
|
content = template.render({'processors': 'no'}, request)
|
||||||
self.assertEqual(content, 'no')
|
self.assertEqual(content, 'no')
|
||||||
|
|
||||||
@ignore_warnings(category=RemovedInDjango110Warning)
|
|
||||||
def test_request_context_conflicts_with_request(self):
|
|
||||||
template = self.engine.from_string('hello')
|
|
||||||
|
|
||||||
request = RequestFactory().get('/')
|
|
||||||
request_context = RequestContext(request)
|
|
||||||
# This doesn't raise an exception.
|
|
||||||
template.render(request_context, request)
|
|
||||||
|
|
||||||
other_request = RequestFactory().get('/')
|
|
||||||
msg = ("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.")
|
|
||||||
with self.assertRaisesMessage(ValueError, msg):
|
|
||||||
template.render(request_context, other_request)
|
|
||||||
|
|
||||||
@override_settings(INSTALLED_APPS=['template_backends.apps.good'])
|
@override_settings(INSTALLED_APPS=['template_backends.apps.good'])
|
||||||
def test_templatetag_discovery(self):
|
def test_templatetag_discovery(self):
|
||||||
engine = DjangoTemplates({
|
engine = DjangoTemplates({
|
||||||
|
|
|
@ -248,12 +248,6 @@ class TemplateResponseTest(SimpleTestCase):
|
||||||
{'foo': 'bar'}).render()
|
{'foo': 'bar'}).render()
|
||||||
self.assertEqual(response.content, b'baryes')
|
self.assertEqual(response.content, b'baryes')
|
||||||
|
|
||||||
@ignore_warnings(category=RemovedInDjango110Warning)
|
|
||||||
def test_render_with_context(self):
|
|
||||||
response = self._response('{{ foo }}{{ processors }}',
|
|
||||||
Context({'foo': 'bar'})).render()
|
|
||||||
self.assertEqual(response.content, b'bar')
|
|
||||||
|
|
||||||
def test_context_processor_priority(self):
|
def test_context_processor_priority(self):
|
||||||
# context processors should be overridden by passed-in context
|
# context processors should be overridden by passed-in context
|
||||||
response = self._response('{{ foo }}{{ processors }}',
|
response = self._response('{{ foo }}{{ processors }}',
|
||||||
|
|
Loading…
Reference in New Issue