Django extract string from [ErrorDetail(string='Test Message', code='invalid')]
You can get the string from the ErrorDetail
Object in this way:-
>>> op = ErrorDetail(string='Value must be valid JSON or key, valued pair.', code='invalid')>>> op.title()'Value must be valid JSON or key, valued pair.'
You should try in your template
{% for error in serializer.amount.errors %} {{ error }}{% endofor %}
But I do not understand why do you use django rest_framework with HTML templates. Rest framework is used for REST APIs which is definitely not this case. For this purpose use rather django.forms
. It really does not make sense to use REST serializer directly rendered to the HTML template.
Links:
As the serializer's errors are in a list, so you have to handle it more preciously,I put a very straight forward and intuitive solution to get the string value from the object.
for key, values in serializers.errors.items(): error = [value[:] for value in values] print(error)
Then you can get all error in a list. Though fields has multiple errors in list. so my code can extract string from ErrorDetail() objects.