REST 장고 프레임 워크에서 사용자 지정 오류 응답을 만들려고합니다.Django REST 사용자 지정 오류
나는
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
, 내 views.py에 다음과 또한 settings.py에 다음과 같은 추가 포함 시켰습니다.
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.AllowAny',
),
'EXCEPTION_HANDLER': 'project.input.utils.custom_exception_handler'
}
예상되는 응답이 없기 때문에 뭔가 빠졌습니다. 400 API 응답의 맞춤 오류 메시지
감사합니다, the documentation에서
확실하지만 JSON 응답을 통해 사용자 정의 오류가 표시됩니다. 필자가 이것을 시도했지만 "글로벌 이름"응답을 얻는 Im은 정의되지 않았습니다. – felix001
rest_framework 응답을 가져와야합니다 : 'from rest_framework.response import Response'. 그리고이 코드는 지정한 구조 (** { 'detail': "잘못된 인수", 'args': [ 'arg1', 'arg2']} **)와 JSON 응답을 반환합니다. 필요한 오류 코드 (** 400 **, 자세한 내용은 [restframework 상태 코드] (http://www.django-rest-framework.org/api-guide/status-codes)를 확인하십시오) – argaen