首页 > 其他分享 >【DRF-03】rest-framework之APIView

【DRF-03】rest-framework之APIView

时间:2024-05-22 21:33:22浏览次数:15  
标签:03 get request self args APIView dispatch framework kwargs

  • 安装djangorestframework

    • pip install djangorestframework
  • 基本流程:url--》视图类--》执行dispatch方法

from rest_framework.views import APIView
from rest_framework.response import Response
 
class TestView(APIView):
    def dispatch(self, request, *args, **kwargs):
        """
        请求到来之后,都要执行dispatch方法,dispatch方法根据请求方式不同触发 get/post/put等方法
         
        注意:APIView中的dispatch方法有好多好多的功能
        """
        return super().dispatch(request, *args, **kwargs)
 
    def get(self, request, *args, **kwargs):
        return Response('GET请求,响应内容')
 
    def post(self, request, *args, **kwargs):
        return Response('POST请求,响应内容')
 
    def put(self, request, *args, **kwargs):
        return Response('PUT请求,响应内容')
  • djangorestframework的APIView分析
def dispatch(self, request, *args, **kwargs):
    """
    `.dispatch()` is pretty much the same as Django's regular dispatch,
    but with extra hooks for startup, finalize, and exception handling.
    """
    self.args = args
    self.kwargs = kwargs
    # 对原生的request进行加工(丰富了一些功能)
    # Request(request,parsers=self.get_parsers(),authenticators=self.get_authenticators(),
    #          negotiator=self.get_content_negotiator(),parser_context=parser_context)

    # request(原生的request,MyAuthentication对象)
    # 获取原生request:request._request
    # 获取认证类的对象:request.BasicAuthentication
    # 1.封装request
    request = self.initialize_request(request, *args, **kwargs)
    self.request = request
    self.headers = self.default_response_headers  # deprecate?

    try:
        # 2.认证
        self.initial(request, *args, **kwargs)

        # Get the appropriate handler method
        if request.method.lower() in self.http_method_names:
            handler = getattr(self, request.method.lower(),
                              self.http_method_not_allowed)
        else:
            handler = self.http_method_not_allowed

        response = handler(request, *args, **kwargs)

    except Exception as exc:
        response = self.handle_exception(exc)

    self.response = self.finalize_response(request, response, *args, **kwargs)
    return self.response
def initialize_request(self, request, *args, **kwargs):
    """
    Returns the initial request object.
    """
    parser_context = self.get_parser_context(request)

    return Request(
        request,
        parsers=self.get_parsers(),
        authenticators=self.get_authenticators(), #  [Foo(),Bar()]
        negotiator=self.get_content_negotiator(),
        parser_context=parser_context
    )
def initial(self, request, *args, **kwargs):
    """
    Runs anything that needs to occur prior to calling the method handler.
    """
    self.format_kwarg = self.get_format_suffix(**kwargs)

    # Perform content negotiation and store the accepted info on the request
    neg = self.perform_content_negotiation(request)
    request.accepted_renderer, request.accepted_media_type = neg

    # Determine the API version, if versioning is in use.
    version, scheme = self.determine_version(request, *args, **kwargs)
    request.version, request.versioning_scheme = version, scheme

    # Ensure that the incoming request is permitted
    # 4.实现认证,权限,频率
    self.perform_authentication(request)
    self.check_permissions(request)
    self.check_throttles(request)
  • djangorestframework的request对象介绍

标签:03,get,request,self,args,APIView,dispatch,framework,kwargs
From: https://www.cnblogs.com/xwltest/p/18207120

相关文章

  • 2022-07-03-含有非期望产出的sbm模型python代码
    传统的径向DEA模型无法考虑“松弛变量”对效率值的影响,也没有考虑同时使期望产出增加,非期望产出减少的技术变化,以此度量的效率值是不准确或有偏的,为了解决这一问题,Tone(2001)提出了基于投入产出松弛变量的环境效率评价模型,简称SBM模型,在此基础上,他进一步提出了SBM的拓展模型,从而实......
  • CSP历年复赛题-P1044 [NOIP2003 普及组] 栈
    原题链接:https://www.luogu.com.cn/problem/P1044题意解读:一组数入栈、出栈的方案数,如果了解卡特兰数,此题可以秒杀;如果不了解,也可以通过递归或者递推来解决;最次,可以通过DFS暴搜出方案数,当然对于n个数,一共有n次入栈、n次出栈,一共2n次,每次要么入栈要么出栈,总搜索次数在22n规模,n最......
  • CSP历年复赛题-P1045 [NOIP2003 普及组] 麦森数
    原题链接:https://www.luogu.com.cn/problem/P1045题意解读:要计算2p-1的位数和最后500位,实际上只需要计算2p,两者位数一致,前者比后者个位减1即可,且个位肯定不会是0,比较容易处理。解题思路:如果直接采用高精度乘法计算2p,p最大3.1*106,高精度所用数组最长大概9*105,一共最多计算3.......
  • CSP历年复赛题-P1043 [NOIP2003 普及组] 数字游戏
    原题链接:https://www.luogu.com.cn/problem/P1043题意解读:将n个环形数分成任意m组,组内求和再%10、负数转正,组间相乘,求所有分组方案中得到结果的最小值和最大值。解题思路:比赛题的首要目的是上分!此题一看就是DP,但是苦苦思索了半天,想不清楚状态表示,那么可以换换策略,先暴力得分再......
  • The following instances are in the device manifest but not specified in framewor
    android 编译hal报错:ERROR:filesareincompatible:Thefollowinginstancesareinthedevicemanifestbutnotspecifiedinframeworkcompatibilitymatrix:android.hardware.hongxi.IHongxi/default(@1)Suggestedfix:1.UpdatedeprecatedHALstothelatestve......
  • 2024-2030数据集成成熟度曲线(一)
    作者|郭炜导读:最新发布的《技术成熟度曲线2024》全面评估数据集成技术架构的7个维度,包括技术成熟度、技术难度、业务价值、技术成熟周期、管理协作难度、大模型结合等评估维度,报告篇幅较长,我们将报告分为3篇系列文章,本文为报告第一篇,描述了「从ETL到ELT,到EtLT的趋势」。接......
  • 20245.05.03
    学习时间1h代码行数30行博客量1篇学习内容<%@pagelanguage="java"import="java.sql.*"pageEncoding="utf-8"%><html><head><title>学生信息管理系统</title><linkrel="stylesheet"type="text/......
  • CSP历年复赛题-P1037 [NOIP2002 普及组] 产生数
    原题链接:https://www.luogu.com.cn/problem/P1037题意解读:一个长整数,有若干数字替换规则,计算可以转换成多少种不同的整数。解题思路:看题之后,第一感觉,是用DFS:1、用字符串存储整数2、用领接表存储数字替换规则,因为一个数字可以替换成多个其他数字3、在dfs中,枚举字符串每个数字......
  • CSP历年复赛题-P1042 [NOIP2003 普及组] 乒乓球
    原题链接:https://www.luogu.com.cn/problem/P1042题意解读:分别针对11分制和21分制,输出每局比分。只需要判断一局的结束条件:得分高者如果达到11或者21,且比分间隔大于等于2分,则表示一局结束,可开始下一局,用模拟法即可解决。100分代码:#include<bits/stdc++.h>usingnamespaces......
  • CSP历年复赛题-P1035 [NOIP2002 普及组] 级数求和
    原题链接:https://www.luogu.com.cn/problem/P1035题意解读:根据公式模拟法求解即可。解题思路:枚举i,计算sum,如果sum>k,则输出i100分代码:#include<bits/stdc++.h>usingnamespacestd;intmain(){intk;cin>>k;doublesum=0;inti=0;while(......