From 127f9e073d83904defd4bde4eaa4491f3306ca25 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sat, 3 Jan 2015 19:28:15 +0100 Subject: [PATCH] Restored support for multiple template names in render(_to_response). This possibility was documented but not tested. It had been broken during the multiple template engines refactor. --- django/shortcuts.py | 5 +++-- tests/shortcuts/tests.py | 10 ++++++++++ tests/shortcuts/urls.py | 2 ++ tests/shortcuts/views.py | 20 ++++++++++++++++++++ 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/django/shortcuts.py b/django/shortcuts.py index a8f7278bcb..b43c23a891 100644 --- a/django/shortcuts.py +++ b/django/shortcuts.py @@ -32,7 +32,7 @@ def render_to_response(template_name, context=None, and dirs is _dirs_undefined and dictionary is _dictionary_undefined): # No deprecated arguments were passed - use the new code path - content = loader.get_template(template_name).render(context) + content = loader.render_to_string(template_name, context) else: # Some deprecated arguments were passed - use the legacy code path @@ -56,7 +56,8 @@ def render(request, template_name, context=None, and dirs is _dirs_undefined and dictionary is _dictionary_undefined): # No deprecated arguments were passed - use the new code path - content = loader.get_template(template_name).render(context, request) + # In Django 2.0, request should become a positional argument. + content = loader.render_to_string(template_name, context, request=request) else: # Some deprecated arguments were passed - use the legacy code path diff --git a/tests/shortcuts/tests.py b/tests/shortcuts/tests.py index 612f4a3dd8..f5351dfa03 100644 --- a/tests/shortcuts/tests.py +++ b/tests/shortcuts/tests.py @@ -14,6 +14,11 @@ class ShortcutTests(TestCase): 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') + @ignore_warnings(category=RemovedInDjango20Warning) def test_render_to_response_with_request_context(self): response = self.client.get('/render_to_response/request_context/') @@ -51,6 +56,11 @@ class ShortcutTests(TestCase): self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8') self.assertFalse(hasattr(response.context.request, 'current_app')) + def test_render_with_multiple_templates(self): + response = self.client.get('/render/multiple_templates/') + self.assertEqual(response.status_code, 200) + self.assertEqual(response.content, b'FOO.BAR../path/to/static/media/\n') + @ignore_warnings(category=RemovedInDjango20Warning) def test_render_with_base_context(self): response = self.client.get('/render/base_context/') diff --git a/tests/shortcuts/urls.py b/tests/shortcuts/urls.py index f02e74a745..cbb6c99c14 100644 --- a/tests/shortcuts/urls.py +++ b/tests/shortcuts/urls.py @@ -4,11 +4,13 @@ from . import views urlpatterns = [ url(r'^render_to_response/$', views.render_to_response_view), + url(r'^render_to_response/multiple_templates/$', views.render_to_response_view_with_multiple_templates), url(r'^render_to_response/request_context/$', views.render_to_response_view_with_request_context), url(r'^render_to_response/content_type/$', views.render_to_response_view_with_content_type), url(r'^render_to_response/dirs/$', views.render_to_response_view_with_dirs), url(r'^render_to_response/context_instance_misuse/$', views.render_to_response_with_context_instance_misuse), url(r'^render/$', views.render_view), + url(r'^render/multiple_templates/$', views.render_view_with_multiple_templates), url(r'^render/base_context/$', views.render_view_with_base_context), url(r'^render/content_type/$', views.render_view_with_content_type), url(r'^render/dirs/$', views.render_with_dirs), diff --git a/tests/shortcuts/views.py b/tests/shortcuts/views.py index 66196f11cf..42493f3e7b 100644 --- a/tests/shortcuts/views.py +++ b/tests/shortcuts/views.py @@ -15,6 +15,16 @@ def render_to_response_view(request): }) +def render_to_response_view_with_multiple_templates(request): + return render_to_response([ + 'shortcuts/no_such_template.html', + 'shortcuts/render_test.html', + ], { + 'foo': 'FOO', + 'bar': 'BAR', + }) + + def render_to_response_view_with_request_context(request): return render_to_response('shortcuts/render_test.html', { 'foo': 'FOO', @@ -50,6 +60,16 @@ def render_view(request): }) +def render_view_with_multiple_templates(request): + return render(request, [ + 'shortcuts/no_such_template.html', + 'shortcuts/render_test.html', + ], { + 'foo': 'FOO', + 'bar': 'BAR', + }) + + def render_view_with_base_context(request): return render(request, 'shortcuts/render_test.html', { 'foo': 'FOO',