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.
This commit is contained in:
Aymeric Augustin 2015-01-03 19:28:15 +01:00
parent eaa1a22341
commit 127f9e073d
4 changed files with 35 additions and 2 deletions

View File

@ -32,7 +32,7 @@ def render_to_response(template_name, context=None,
and dirs is _dirs_undefined and dirs is _dirs_undefined
and dictionary is _dictionary_undefined): and dictionary is _dictionary_undefined):
# No deprecated arguments were passed - use the new code path # 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: else:
# Some deprecated arguments were passed - use the legacy code path # 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 dirs is _dirs_undefined
and dictionary is _dictionary_undefined): and dictionary is _dictionary_undefined):
# No deprecated arguments were passed - use the new code path # 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: else:
# Some deprecated arguments were passed - use the legacy code path # Some deprecated arguments were passed - use the legacy code path

View File

@ -14,6 +14,11 @@ class ShortcutTests(TestCase):
self.assertEqual(response.content, b'FOO.BAR..\n') self.assertEqual(response.content, b'FOO.BAR..\n')
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8') 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) @ignore_warnings(category=RemovedInDjango20Warning)
def test_render_to_response_with_request_context(self): def test_render_to_response_with_request_context(self):
response = self.client.get('/render_to_response/request_context/') 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.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')
self.assertFalse(hasattr(response.context.request, 'current_app')) 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) @ignore_warnings(category=RemovedInDjango20Warning)
def test_render_with_base_context(self): def test_render_with_base_context(self):
response = self.client.get('/render/base_context/') response = self.client.get('/render/base_context/')

View File

@ -4,11 +4,13 @@ from . import views
urlpatterns = [ urlpatterns = [
url(r'^render_to_response/$', views.render_to_response_view), 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/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/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/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_to_response/context_instance_misuse/$', views.render_to_response_with_context_instance_misuse),
url(r'^render/$', views.render_view), 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/base_context/$', views.render_view_with_base_context),
url(r'^render/content_type/$', views.render_view_with_content_type), url(r'^render/content_type/$', views.render_view_with_content_type),
url(r'^render/dirs/$', views.render_with_dirs), url(r'^render/dirs/$', views.render_with_dirs),

View File

@ -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): def render_to_response_view_with_request_context(request):
return render_to_response('shortcuts/render_test.html', { return render_to_response('shortcuts/render_test.html', {
'foo': 'FOO', '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): def render_view_with_base_context(request):
return render(request, 'shortcuts/render_test.html', { return render(request, 'shortcuts/render_test.html', {
'foo': 'FOO', 'foo': 'FOO',