[Django]-Django Rest Framework how to return detail not found on all invalid urls?

3👍

You can write a custom 404 view and assign it to django’s handler404. This could look like this (not tested):

urls.py

from django.conf.urls import handler404
from myapp import views

handler404 = views.error_page

views.py

from rest_framework.decorators import api_view
from rest_framework import status

@api_view()
def error_page(request):
    return Response({'detail': 'Not found'}, status=status.HTTP_404_NOT_FOUND)

Leave a comment