DJango middleware that returns 404s as JSON instead of default html response
When using Django for a restuful API, for example with DRF, it makes sense to return errors as JSON, which is what happens with 400s, 403s, etc. since they are generated by DRF themselves i.e serializer.is_valid(raise_exception=True)
This however is not the case with 404s.
This middleware remedies this problem by intercepting 404 responses and turning them into JSON responses.
Please note that middleware changed in django 1.10 and thus this pacakge requires django version >= 1.10.
MIDDLEWARE = [
# Should come after any django middleware such as CommonMiddleware
'django_json_404_middleware.JSON404Middleware',
]
pip install django-json-404-middleware