Django自定义封装Response
定义Response类
from rest_framework.response import Response
class APIResponse(Response):def __init__(self, code=100, msg='成功', status=None, headers=None, **kwargs):data = {'code': code, 'msg': msg}if kwargs:data.update(kwargs)super().__init__(data=data, status=status, headers=headers)
视图层示例
from rest_framework.views import APIView
from utils.common_response import APIResponseclass LoggerView(APIView):def get(self, request):info = ('这是一条info级别日志')return APIResponse(msg=info, headers={'name': 'Anna'})