Django No. 17-How to provide a standardized interface return value

I. Introduction

First of all, the overall development idea is: For example, a front-end developer A and a back-end developer B will jointly debug a function of [query user data], and then the front-end developer A will be responsible for the development of [page for querying user data]. Back-end developer B is responsible for the development of the [interface for querying user data] and after the development is completed and the self-test is passed, it is provided to front-end developer A for joint debugging.

We need to know that the prerequisite for front-end developer A and back-end developer B to develop this function together is to strictly develop requirements documents and prototype diagrams written by product personnel.

Therefore, in the actual development process, the front-end developer A will require the return value of the [query user data interface] provided by the back-end developer to meet these two points:

⑴. All fields and field value information required by front-end developer A must be returned, because front-end developers can get these data to conduct self-test to ensure that the data is not missed;

⑵. The data structure of each field value must meet the requirements of front-end developer A, because front-end developer A will get such field values ​​directly to do related processing without the need to perform secondary processing on the data structure of field values. Improving the development efficiency of front-end developer A can also improve the efficiency of data loading.

detail:

①. In the django framework, the interface is actually a view function, but the name is inconsistent.

②. The follow-up involved front-end and back-end interface joint debugging process can basically be carried out according to this idea.

 

2. Carry out an actual complete process operation

2.1. The first step: write a view function for querying user data


 

def search_person(request):

    # 进行实际的一个完整流程操作

    try:

        res = Person.objects.all().values()

        res = list(res)

    except:

        raise ("查询语句执行过程中出现报错信息,程序终止执行!")

    # 初始化一个空dict,用于存储要返回给起前端页面的所有字段和对应字段值,且字段对应字典的key,字段值对应字典的value;

    data = {}



    if len(res) != 0:

        # 第一步:把数据表的相关数据都存储在这个key【result】

        data["result"] = res

        # 第二步:把状态码200的值存储在这个key【code】

        data["code"] = 200

        # 第三步:把状态success的值存储在这个key【success】

        data["success"] = True

        # 第四步:把信息的值存储在这个key【msg】

        data["msg"] = "获取数据成功!"

    else:

        data["result"] = res

        data["code"] = 100

        data["success"] = False

        data["msg"] = "获取不到数据"

    return JsonResponse(data, json_dumps_params={"ensure_ascii": False})

2.2. Step 2: Write a corresponding URL matching rule

detail:

①. The url matching rules have been configured before, and the corresponding content can be found in the previous blog posts. I will not continue to write them again in this blog;

2.3. The third step: start the django project [helloworld] service

2.4. Step 4: Visit the url address [ http://127.0.0.1:8000/search_person ] through any browser to view the display data

2.5. Step 5: Copy the return value of the interface to [ https://www.json.cn/ ] for online analysis and formatting of json data, which is convenient and intuitive to view the data

{

    "result":[

        {

            "id":1,

            "name":"杨见文",

            "age":25

        },

        {

            "id":2,

            "name":"廖旺良",

            "age":26

        },

        {

            "id":3,

            "name":"王凯",

            "age":28

        },

        {

            "id":4,

            "name":"陈凯",

            "age":28

        }

    ],

    "code":200,

    "success":true,

    "msg":"获取数据成功!"

}

 

Guess you like

Origin blog.csdn.net/LYX_WIN/article/details/114520519