封装Response对象
封装Response对象
#utils.py
#自定制响应
from rest_framework.response import Response
class CommonResponse(Response):
def __init__(self, code=10, msg='成功', data=None, **kwargs):
dic = {'code': code, 'msg': msg, 'data': data}
if data:
dic = {'code': code, 'msg': msg, 'data': data}
dic.update(kwargs)
super().__init__(data=dic, status=None, headers=None, )
#urls.py
path('test7/', views.Test7View.as_view()),
#views.py
from app01.utils import CommonResponse
class Test7View(APIView):
def get(self, request, *args, **kwargs):
return CommonResponse(data={'name':'zhao'},demo='kfdha',token='fldksjafld')
#views.py
from app01.utils import CommonResponse
class Test7View(APIView):
def get(self, request, *args, **kwargs):
return CommonResponse(token='fldksjafld')
class Test7View(APIView):
def get(self, request, *args, **kwargs):
return CommonResponse(code=101,msg='错误',data={'name':'lisi'},aa='falkda')
本文来自博客园,作者:ExpiredSaury,转载请注明原文链接:https://www.cnblogs.com/saury/p/16976715.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用