1 回答

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊
您可以實(shí)現(xiàn)自定義異常處理程序:
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
要為您的項(xiàng)目應(yīng)用此處理程序,請將其添加到REST_FRAMEWORK設(shè)置中:
REST_FRAMEWORK = {
'EXCEPTION_HANDLER': 'my_project.my_app.utils.custom_exception_handler'
}
添加回答
舉報(bào)