我正在尝试从REST Django框架创建自定义错误响应.
我在views.py中包含了以下内容,
from rest_framework.views import exception_handler def custom_exception_handler(exc): """ Custom exception handler for Django Rest Framework that adds the `status_code` to the response and renames the `detail` key to `error`. """ response = exception_handler(exc) if response is not None: response.data['status_code'] = response.status_code response.data['error'] = response.data['detail'] response.data['detail'] = "CUSTOM ERROR" return response
REST_FRAMEWORK = { 'DEFAULT_PERMISSION_CLASSES': ( 'rest_framework.permissions.AllowAny',),'EXCEPTION_HANDLER': 'project.input.utils.custom_exception_handler' }
我错过了什么,因为我没有得到预期的回应.即400 API响应中的自定义错误消息.
谢谢,