zoukankan      html  css  js  c++  java
  • 自定义Response

    应用下新建utils文件,在里面定义自己的Response类,继承自Response

    # 导入Response
    from rest_framework.response import Response
    
    # 自定义的Response类
    class APIResponse(Response):
        # 初始化属性
        def __init__(self, code=100, msg='成功', data=None, status=None, headers=None, **kwargs):  #  **kwargs用于接受其他的自定义数据
            dic = {'code': code, 'msg': msg}
            if data:
                dic = {'code': code, 'msg': msg, 'data': data}
            else:
                dic.update(kwargs)
            super().__init__(data=dic, status=status, headers=headers)
    -views.py
    
    # 自定义的response
    from app01.utils import APIResponse
    
    class APIViewresponse(GenericAPIView):
        queryset = Books.objects.all()
        serializer_class = BookSerializer
    
        def get(self, request):
            book_list = self.get_queryset()
            book_ser = self.get_serializer(book_list, many=True)
            return APIResponse(100, '成功', book_ser.data)  # code,msg,data就可以随意定制
  • 相关阅读:
    Commix命令注入漏洞利用
    漏洞扫描
    信息搜集神器
    peepingtom
    自动化安装一些渗透工具的脚本
    MITMF
    Xssf配合metaspolit使用
    Beef安装与简单使用
    Linux安全检测常用方法
    cobaltstrike3.8服务器搭建及使用
  • 原文地址:https://www.cnblogs.com/qjk95/p/13280868.html
Copyright © 2011-2022 走看看