mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-10 19:56:59 +03:00
Errors in browsable API on error. Closes #3024.
This commit is contained in:
parent
6e6fa893e4
commit
90fe0fb881
|
@ -14,6 +14,7 @@ from django.utils.translation import ugettext_lazy as _
|
||||||
from django.utils.translation import ungettext
|
from django.utils.translation import ungettext
|
||||||
|
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
|
from rest_framework.utils.serializer_helpers import ReturnDict, ReturnList
|
||||||
|
|
||||||
|
|
||||||
def _force_text_recursive(data):
|
def _force_text_recursive(data):
|
||||||
|
@ -22,14 +23,20 @@ def _force_text_recursive(data):
|
||||||
lazy translation strings into plain text.
|
lazy translation strings into plain text.
|
||||||
"""
|
"""
|
||||||
if isinstance(data, list):
|
if isinstance(data, list):
|
||||||
return [
|
ret = [
|
||||||
_force_text_recursive(item) for item in data
|
_force_text_recursive(item) for item in data
|
||||||
]
|
]
|
||||||
|
if isinstance(data, ReturnList):
|
||||||
|
return ReturnList(ret, serializer=data.serializer)
|
||||||
|
return data
|
||||||
elif isinstance(data, dict):
|
elif isinstance(data, dict):
|
||||||
return dict([
|
ret = dict([
|
||||||
(key, _force_text_recursive(value))
|
(key, _force_text_recursive(value))
|
||||||
for key, value in data.items()
|
for key, value in data.items()
|
||||||
])
|
])
|
||||||
|
if isinstance(data, ReturnDict):
|
||||||
|
return ReturnDict(ret, serializer=data.serializer)
|
||||||
|
return data
|
||||||
return force_text(data)
|
return force_text(data)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -204,7 +204,7 @@ class BaseSerializer(Field):
|
||||||
self._errors = {}
|
self._errors = {}
|
||||||
|
|
||||||
if self._errors and raise_exception:
|
if self._errors and raise_exception:
|
||||||
raise ValidationError(self._errors)
|
raise ValidationError(self.errors)
|
||||||
|
|
||||||
return not bool(self._errors)
|
return not bool(self._errors)
|
||||||
|
|
||||||
|
|
|
@ -145,6 +145,16 @@ class TestRootView(TestCase):
|
||||||
created = self.objects.get(id=4)
|
created = self.objects.get(id=4)
|
||||||
self.assertEqual(created.text, 'foobar')
|
self.assertEqual(created.text, 'foobar')
|
||||||
|
|
||||||
|
def test_post_error_root_view(self):
|
||||||
|
"""
|
||||||
|
POST requests to ListCreateAPIView in HTML should include a form error.
|
||||||
|
"""
|
||||||
|
data = {'text': 'foobar' * 100}
|
||||||
|
request = factory.post('/', data, HTTP_ACCEPT='text/html')
|
||||||
|
response = self.view(request).render()
|
||||||
|
expected_error = '<span class="help-block">Ensure this field has no more than 100 characters.</span>'
|
||||||
|
self.assertIn(expected_error, response.rendered_content)
|
||||||
|
|
||||||
|
|
||||||
EXPECTED_QUERIES_FOR_PUT = 3 if django.VERSION < (1, 6) else 2
|
EXPECTED_QUERIES_FOR_PUT = 3 if django.VERSION < (1, 6) else 2
|
||||||
|
|
||||||
|
@ -282,6 +292,16 @@ class TestInstanceView(TestCase):
|
||||||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
|
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
|
||||||
self.assertFalse(self.objects.filter(id=999).exists())
|
self.assertFalse(self.objects.filter(id=999).exists())
|
||||||
|
|
||||||
|
def test_put_error_instance_view(self):
|
||||||
|
"""
|
||||||
|
Incorrect PUT requests in HTML should include a form error.
|
||||||
|
"""
|
||||||
|
data = {'text': 'foobar' * 100}
|
||||||
|
request = factory.put('/', data, HTTP_ACCEPT='text/html')
|
||||||
|
response = self.view(request, pk=1).render()
|
||||||
|
expected_error = '<span class="help-block">Ensure this field has no more than 100 characters.</span>'
|
||||||
|
self.assertIn(expected_error, response.rendered_content)
|
||||||
|
|
||||||
|
|
||||||
class TestFKInstanceView(TestCase):
|
class TestFKInstanceView(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user