zl程序教程

您现在的位置是:首页 >  前端

当前栏目

rest_framework 的 Response响应封装

响应封装 Framework rest response
2023-09-27 14:25:19 时间
from rest_framework.response import Response


# 公用的响应
class APIRespones(Response):

    """
    Respones({
        'status':0,
        'msg':'ok',
        'results':[],
        'token':'',
    })
    """
    def __init__(self, data_status=0, data_msg='ok', res_status=True, results=None, http_status=None,
                 headers=None, exception=False, **kwargs):
        data = {
            'status': data_status,
            'msg': data_msg,
            'succese': res_status,
        }
        if results is not None:
            data['results'] = results
        data.update(kwargs)
        super().__init__(data=data, status=http_status, headers=headers, exception=exception)

 

 

调用

def post(self,request):

    return APIRespones(status=1000, data_msg=f'story调用成功')