'How to send all django error as json data

I am building a rest api in django, I am using postman to test my apis, Everything is great though I want if any error occurs in my django app so django send me a json error rather than a html page. Is there a way to do that.



Solution 1:[1]

What you want is implementing your own Exception handler : https://www.django-rest-framework.org/api-guide/exceptions/

This works for any type of exceptions, and in the format you want.

Note that some exceptions are already nicely handled, like for instance with serializers and generic API, where responses are sent as json. An example can be missing required fields on a serializers or validation error, a bad permission, etc.

You can replace 400 and 500 handlers too to fill your purposes.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sami Tahri