Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions rest_framework/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,11 @@ def resolve_template(self, template_names):

def get_template_context(self, data, renderer_context):
response = renderer_context['response']
# in case a ValidationError is caught the data parameter may be a list
# see rest_framework.views.exception_handler
# data may be a list when a list view is used or when a ValidationError
# is raised; wrap it in a dict so Django's template engine can accept it.
# Use 'results' to stay consistent with paginated response conventions.
if isinstance(data, list):
return {'details': data, 'status_code': response.status_code}
return {'results': data, 'status_code': response.status_code}
if response.exception:
data['status_code'] = response.status_code
return data
Expand Down
34 changes: 34 additions & 0 deletions tests/test_htmlrenderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,19 @@ def validation_error(request):
raise ValidationError('error')


@api_view(('GET',))
@renderer_classes((TemplateHTMLRenderer,))
def list_view(request):
data = [{'name': 'foo'}, {'name': 'bar'}]
return Response(data, template_name='list.html')


urlpatterns = [
path('', example),
path('permission_denied', permission_denied),
path('not_found', not_found),
path('validation_error', validation_error),
path('list', list_view),
]


Expand All @@ -71,6 +79,10 @@ def get_template(template_name, dirs=None):
def select_template(template_name_list, dirs=None, using=None):
if template_name_list == ['example.html']:
return engines['django'].from_string("example: {{ object }}")
if template_name_list == ['list.html']:
return engines['django'].from_string(
"{% for item in results %}{{ item.name }}{% endfor %}"
)
raise TemplateDoesNotExist(template_name_list[0])

django.template.loader.get_template = get_template
Expand Down Expand Up @@ -105,6 +117,28 @@ def test_validation_error_html_view(self):
self.assertEqual(response.content, b"400 Bad Request")
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')

def test_list_view_with_template_html_renderer(self):
response = self.client.get('/list')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertContains(response, 'foo')
self.assertContains(response, 'bar')
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')

def test_get_template_context_wraps_list_under_results_key(self):
renderer = TemplateHTMLRenderer()

class MockResponse:
template_name = None
exception = False
status_code = 200

context = renderer.get_template_context(
[{'name': 'foo'}], {'response': MockResponse()}
)
self.assertIn('results', context)
self.assertEqual(context['results'], [{'name': 'foo'}])
self.assertNotIn('details', context)

# 2 tests below are based on order of if statements in corresponding method
# of TemplateHTMLRenderer
def test_get_template_names_returns_own_template_name(self):
Expand Down
Loading