1 回答
TA贡献1865条经验 获得超7个赞
您可以实现自定义异常处理程序:
from rest_framework.views import exception_handler
def custom_exception_handler(exc, context):
# Call REST framework's default exception handler first,
# to get the standard error response.
response = exception_handler(exc, context)
# Now add the HTTP status code to the response.
if response is not None and response.status_code == 404:
response.data = {
"message": "Instance not found.",
"error": "HTTP_404_NOT_FOUND",
}
return response
要为您的项目应用此处理程序,请将其添加到REST_FRAMEWORK设置中:
REST_FRAMEWORK = {
'EXCEPTION_HANDLER': 'my_project.my_app.utils.custom_exception_handler'
}
添加回答
举报