在Django Rest中自定义错误消息 [英] customize error message in django rest

查看:48
本文介绍了在Django Rest中自定义错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自定义django rest框架的错误响应.这是来自django rest的诽谤错误响应..

i want to customize the error response from django rest framework. this is slandered error response from django rest..

{
    "style": [
        "This field is required."
    ], 
    "code": [
        "code must be in 60 to 120 chars."
    ]
}  

我想像自定义它....

i want to customize it like....

{
    "style": [
        "error_code":"20"
        "error_message":"This field is required."
    ], 
    "code": [
        "error_code":"22"
        "error_message":"code must be in 60 to 120 chars."
    ]
}  

推荐答案

我在显示错误时遇到了同样的问题.首先,您应该知道您不能在列表中使用键值对(即样式":[["error_code":"20","error_message":"必填."] ),如果要将错误转换为此自定义类型,则应将类型更改为字典.一种简单的方法是,您可以拥有自己的自定义异常处理程序,然后告诉rest框架使用该异常处理程序来自定义您的异常.首先,您应该在项目 settings.py 中添加以下行:

I had the same problem in showing errors. First of all you should know that you can't use key-value pairs in a list (i.e. "style": ["error_code": "20", "error_message": "This field is required."]) and you should change your type to dictionary if you want to convert your error to this custom type. One easy way is that you can have your own custom exception handler and then tell rest framework to use that exception handler to customize your exceptions. First you should add the following line in your project settings.py:

REST_FRAMEWORK = {
    'EXCEPTION_HANDLER': 'utils.exception_handlers.custom_exception_handler', # You should add the path to your custom exception handler here.
}

这告诉所有异常都应通过此处理程序.之后,您应该添加python文件并在其中添加代码(使用前面提到的 settings.py 中的此文件路径).在以下代码中,您可以看到此处理程序的示例:

Which tells that all of the exceptions should pass through this handler. After that you should add python file and add your codes in it (use this file path in your settings.py which mentioned before). In following code you can see an example of this handler:

from rest_framework.exceptions import ErrorDetail
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.
    # Call REST framework's default exception handler first,
    # to get the standard error response.
    response = exception_handler(exc, context)
    custom_data = {}

    if isinstance(response.data, dict):
        for key, value in response.data.items():
            if value and isinstance(value, list) and isinstance(value[0], ErrorDetail):
                custom_response[key] = {
                    "error_message": str(value[0]),
                    "error_code": response.status_code # or any custom code that you need
                }
            else:
                break

    if custom_data:
        response.data = custom_data
    return response

注意:这是一个简单的示例,您应该测试API以确保一切正常.

Note: This was a quick example and you should test your APIs to make sure that everything works.

这篇关于在Django Rest中自定义错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆