mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-22 01:26:53 +03:00
Fix get_template_context to handle also lists (#9467)
This commit is contained in:
parent
ccfe0a9637
commit
f74185b6dd
|
@ -171,6 +171,10 @@ class TemplateHTMLRenderer(BaseRenderer):
|
||||||
|
|
||||||
def get_template_context(self, data, renderer_context):
|
def get_template_context(self, data, renderer_context):
|
||||||
response = renderer_context['response']
|
response = renderer_context['response']
|
||||||
|
# in case a ValidationError is caught the data parameter may be a list
|
||||||
|
# see rest_framework.views.exception_handler
|
||||||
|
if isinstance(data, list):
|
||||||
|
return {'details': data, 'status_code': response.status_code}
|
||||||
if response.exception:
|
if response.exception:
|
||||||
data['status_code'] = response.status_code
|
data['status_code'] = response.status_code
|
||||||
return data
|
return data
|
||||||
|
|
|
@ -8,6 +8,7 @@ from django.urls import path
|
||||||
|
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
from rest_framework.decorators import api_view, renderer_classes
|
from rest_framework.decorators import api_view, renderer_classes
|
||||||
|
from rest_framework.exceptions import ValidationError
|
||||||
from rest_framework.renderers import TemplateHTMLRenderer
|
from rest_framework.renderers import TemplateHTMLRenderer
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
|
|
||||||
|
@ -34,10 +35,17 @@ def not_found(request):
|
||||||
raise Http404()
|
raise Http404()
|
||||||
|
|
||||||
|
|
||||||
|
@api_view(('GET',))
|
||||||
|
@renderer_classes((TemplateHTMLRenderer,))
|
||||||
|
def validation_error(request):
|
||||||
|
raise ValidationError('error')
|
||||||
|
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', example),
|
path('', example),
|
||||||
path('permission_denied', permission_denied),
|
path('permission_denied', permission_denied),
|
||||||
path('not_found', not_found),
|
path('not_found', not_found),
|
||||||
|
path('validation_error', validation_error),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@ -91,6 +99,12 @@ class TemplateHTMLRendererTests(TestCase):
|
||||||
self.assertEqual(response.content, b"403 Forbidden")
|
self.assertEqual(response.content, b"403 Forbidden")
|
||||||
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')
|
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')
|
||||||
|
|
||||||
|
def test_validation_error_html_view(self):
|
||||||
|
response = self.client.get('/validation_error')
|
||||||
|
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||||
|
self.assertEqual(response.content, b"400 Bad Request")
|
||||||
|
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')
|
||||||
|
|
||||||
# 2 tests below are based on order of if statements in corresponding method
|
# 2 tests below are based on order of if statements in corresponding method
|
||||||
# of TemplateHTMLRenderer
|
# of TemplateHTMLRenderer
|
||||||
def test_get_template_names_returns_own_template_name(self):
|
def test_get_template_names_returns_own_template_name(self):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user