Reputation: 87
I would like to replace default django 404 (and other in the future) with the DRF response, as easy as it is possible, just to return (standard DRF response):
{
"detail": "Not found."
}
So I put this code in my url.py file.
from rest_framework.exceptions import NotFound
from django.conf.urls import handler404
handler404 = NotFound
When I set Debug flag (in the set Ture I'm geting 500, when set it to False 404 but in the html form.
I've read doc here: http://www.django-rest-framework.org/api-guide/exceptions/ but to be honest I don't know how to put this into reality.
it seems to be easy, but somehow I failed to implement this (above code comes form the other SO threads).
Thank you in advance
Upvotes: 5
Views: 2050
Reputation: 2550
You should create a custom view for the 404 then to use it. for example:
in views.py
:
from django.http import HttpResponseNotFound
import json
def error404(request, exception):
response_data = {}
response_data['detail'] = 'Not found.'
return HttpResponseNotFound(json.dumps(response_data), content_type="application/json")
then in urls.py
:
from django.conf.urls import handler404
from yourapp.views import error404
handler404 = error404
Upvotes: 2