diff --git a/django/shortcuts.py b/django/shortcuts.py index 04e2f3e103..c489ecd5cd 100644 --- a/django/shortcuts.py +++ b/django/shortcuts.py @@ -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 for convenience's sake. """ +import warnings + from django.http import ( Http404, HttpResponse, HttpResponsePermanentRedirect, HttpResponseRedirect, ) from django.template import loader from django.urls import NoReverseMatch, reverse +from django.utils.deprecation import RemovedInDjango30Warning from django.utils.encoding import force_text 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 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) return HttpResponse(content, content_type, status) diff --git a/docs/internals/deprecation.txt b/docs/internals/deprecation.txt index d6123b6b53..56aa7adacf 100644 --- a/docs/internals/deprecation.txt +++ b/docs/internals/deprecation.txt @@ -17,6 +17,8 @@ details on these changes. * The ``django.db.backends.postgresql_psycopg2`` module will be removed. +* ``django.shortcuts.render_to_response()`` will be removed. + .. _deprecation-removed-in-2.1: 2.1 diff --git a/docs/releases/2.0.txt b/docs/releases/2.0.txt index 818a8ada87..c4f65ec9ac 100644 --- a/docs/releases/2.0.txt +++ b/docs/releases/2.0.txt @@ -297,6 +297,10 @@ Miscellaneous ``'django.db.backends.postgresql_psycopg2'``, though you can simplify that by 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: Features removed in 2.0 diff --git a/docs/topics/http/shortcuts.txt b/docs/topics/http/shortcuts.txt index 705de11b69..51a39c88a1 100644 --- a/docs/topics/http/shortcuts.txt +++ b/docs/topics/http/shortcuts.txt @@ -86,9 +86,11 @@ This example is equivalent to:: .. 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 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()`` ============== diff --git a/tests/shortcuts/test_render_to_response.py b/tests/shortcuts/test_render_to_response.py new file mode 100644 index 0000000000..7bf7ac9ad6 --- /dev/null +++ b/tests/shortcuts/test_render_to_response.py @@ -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') diff --git a/tests/shortcuts/tests.py b/tests/shortcuts/tests.py index ec79226446..fe68d67767 100644 --- a/tests/shortcuts/tests.py +++ b/tests/shortcuts/tests.py @@ -2,41 +2,8 @@ from django.test import SimpleTestCase, override_settings from django.test.utils import require_jinja2 -@override_settings( - ROOT_URLCONF='shortcuts.urls', -) -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') +@override_settings(ROOT_URLCONF='shortcuts.urls') +class RenderTests(SimpleTestCase): def test_render(self): response = self.client.get('/render/')