Fixed #25978 -- Deprecated shorcuts.render_to_response().

This commit is contained in:
Tim Graham 2017-02-11 05:49:04 -05:00 committed by GitHub
parent 0166dd2f8c
commit 7b6e42089c
6 changed files with 58 additions and 36 deletions

View File

@ -3,11 +3,14 @@ This module collects helper functions and classes that "span" multiple levels
of MVC. In other words, these functions/classes introduce controlled coupling of MVC. In other words, these functions/classes introduce controlled coupling
for convenience's sake. for convenience's sake.
""" """
import warnings
from django.http import ( from django.http import (
Http404, HttpResponse, HttpResponsePermanentRedirect, HttpResponseRedirect, Http404, HttpResponse, HttpResponsePermanentRedirect, HttpResponseRedirect,
) )
from django.template import loader from django.template import loader
from django.urls import NoReverseMatch, reverse from django.urls import NoReverseMatch, reverse
from django.utils.deprecation import RemovedInDjango30Warning
from django.utils.encoding import force_text from django.utils.encoding import force_text
from django.utils.functional import Promise from django.utils.functional import Promise
@ -17,6 +20,11 @@ def render_to_response(template_name, context=None, content_type=None, status=No
Returns a HttpResponse whose content is filled with the result of calling Returns a HttpResponse whose content is filled with the result of calling
django.template.loader.render_to_string() with the passed arguments. django.template.loader.render_to_string() with the passed arguments.
""" """
warnings.warn(
'render_to_response() is deprecated in favor render(). It has the '
'same signature except that it also requires a request.',
RemovedInDjango30Warning, stacklevel=2,
)
content = loader.render_to_string(template_name, context, using=using) content = loader.render_to_string(template_name, context, using=using)
return HttpResponse(content, content_type, status) return HttpResponse(content, content_type, status)

View File

@ -17,6 +17,8 @@ details on these changes.
* The ``django.db.backends.postgresql_psycopg2`` module will be removed. * The ``django.db.backends.postgresql_psycopg2`` module will be removed.
* ``django.shortcuts.render_to_response()`` will be removed.
.. _deprecation-removed-in-2.1: .. _deprecation-removed-in-2.1:
2.1 2.1

View File

@ -297,6 +297,10 @@ Miscellaneous
``'django.db.backends.postgresql_psycopg2'``, though you can simplify that by ``'django.db.backends.postgresql_psycopg2'``, though you can simplify that by
using the ``'django.db.backends.postgresql'`` name added in Django 1.9. using the ``'django.db.backends.postgresql'`` name added in Django 1.9.
* ``django.shortcuts.render_to_response()`` is deprecated in favor of
:func:`django.shortcuts.render`. ``render()`` takes the same arguments
except that is also requires a ``request``.
.. _removed-features-2.0: .. _removed-features-2.0:
Features removed in 2.0 Features removed in 2.0

View File

@ -86,9 +86,11 @@ This example is equivalent to::
.. function:: render_to_response(template_name, context=None, content_type=None, status=None, using=None) .. function:: render_to_response(template_name, context=None, content_type=None, status=None, using=None)
.. deprecated:: 2.0
This function preceded the introduction of :func:`render` and works This function preceded the introduction of :func:`render` and works
similarly except that it doesn't make the ``request`` available in the similarly except that it doesn't make the ``request`` available in the
response. It's not recommended and is likely to be deprecated in the future. response.
``redirect()`` ``redirect()``
============== ==============

View File

@ -0,0 +1,39 @@
from django.test import SimpleTestCase, ignore_warnings, override_settings
from django.test.utils import require_jinja2
from django.utils.deprecation import RemovedInDjango30Warning
@ignore_warnings(category=RemovedInDjango30Warning)
@override_settings(ROOT_URLCONF='shortcuts.urls')
class RenderToResponseTests(SimpleTestCase):
def test_render_to_response(self):
response = self.client.get('/render_to_response/')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'FOO.BAR..\n')
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')
def test_render_to_response_with_multiple_templates(self):
response = self.client.get('/render_to_response/multiple_templates/')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'FOO.BAR..\n')
def test_render_to_response_with_content_type(self):
response = self.client.get('/render_to_response/content_type/')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'FOO.BAR..\n')
self.assertEqual(response['Content-Type'], 'application/x-rendertest')
def test_render_to_response_with_status(self):
response = self.client.get('/render_to_response/status/')
self.assertEqual(response.status_code, 403)
self.assertEqual(response.content, b'FOO.BAR..\n')
@require_jinja2
def test_render_to_response_with_using(self):
response = self.client.get('/render_to_response/using/')
self.assertEqual(response.content, b'DTL\n')
response = self.client.get('/render_to_response/using/?using=django')
self.assertEqual(response.content, b'DTL\n')
response = self.client.get('/render_to_response/using/?using=jinja2')
self.assertEqual(response.content, b'Jinja2\n')

View File

@ -2,41 +2,8 @@ from django.test import SimpleTestCase, override_settings
from django.test.utils import require_jinja2 from django.test.utils import require_jinja2
@override_settings( @override_settings(ROOT_URLCONF='shortcuts.urls')
ROOT_URLCONF='shortcuts.urls', class RenderTests(SimpleTestCase):
)
class ShortcutTests(SimpleTestCase):
def test_render_to_response(self):
response = self.client.get('/render_to_response/')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'FOO.BAR..\n')
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')
def test_render_to_response_with_multiple_templates(self):
response = self.client.get('/render_to_response/multiple_templates/')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'FOO.BAR..\n')
def test_render_to_response_with_content_type(self):
response = self.client.get('/render_to_response/content_type/')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, b'FOO.BAR..\n')
self.assertEqual(response['Content-Type'], 'application/x-rendertest')
def test_render_to_response_with_status(self):
response = self.client.get('/render_to_response/status/')
self.assertEqual(response.status_code, 403)
self.assertEqual(response.content, b'FOO.BAR..\n')
@require_jinja2
def test_render_to_response_with_using(self):
response = self.client.get('/render_to_response/using/')
self.assertEqual(response.content, b'DTL\n')
response = self.client.get('/render_to_response/using/?using=django')
self.assertEqual(response.content, b'DTL\n')
response = self.client.get('/render_to_response/using/?using=jinja2')
self.assertEqual(response.content, b'Jinja2\n')
def test_render(self): def test_render(self):
response = self.client.get('/render/') response = self.client.get('/render/')