I want to make an error handler exception to return the error 404 instead of this html. how can i do this ?
我try 了下面的代码,但它对我不起作用
from rest_framework.views import exception_handler
from rest_framework.response import Response
from rest_framework import status
from django.http import Http404
def custom_exception_handler(exc, context):
# Call the default exception handler first,
# to get the standard error response.
response = exception_handler(exc, context)
# Now add the HTTP 404 handling
if isinstance(exc, Http404):
custom_response_data = { "error": "page not found" }
return Response(custom_response_data, status=status.HTTP_404_NOT_FOUND)
# Return the default response if the exception handled is not a Http404
return response