(1)dispatch方法详解----封装原有的request对象

(原request中的方法和属性均可直接在封装后的request中调用,或者使用request._request也可,如:request.user == request._request.user

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
#1.对原来的request进行进一步封装
request = self.initialize_request(request, *args, **kwargs)
self.request = request#(2)request已经是经过进一步封装的
self.headers = self.default_response_headers # deprecate? try:
#2.增加对request的调用
self.initial(request, *args, **kwargs)#(3)比View中多的执行的方法,使用封装过后的request进行调用 # 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)#(4)对原响应对象进行进一步封装
return self.response

dispatch方法重写

(2)initialize_request对原生request进行封装  

def initialize_request(self, request, *args, **kwargs):
"""
Returns the initial request object.
"""
parser_context = self.get_parser_context(request) return Request(
request,#(1-1)原来的request
parsers=self.get_parsers(),#(1-2)[反序列化方式列表]
authenticators=self.get_authenticators(),#(1-3)实例化调用了rest_framework配置文件[中间件的认证类列表]---[BasicAuthentication对象,SessionAuthentication对象]
negotiator=self.get_content_negotiator(),
parser_context=parser_context
)

initialize_request封装request

(3)self.initial详解----认证+权限+节流+版本控制  

self.initial(request, *args, **kwargs)#(3)比View中多的执行的方法,使用封装过后的request进行调用

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.(3-0)api版本的获取
version, scheme = self.determine_version(request, *args, **kwargs)
request.version, request.versioning_scheme = version, scheme#版本号和版本处理对象 # Ensure that the incoming request is permitted
self.perform_authentication(request)#(3-1)进行认证是否登录
self.check_permissions(request)#(3-2)权限组件
self.check_throttles(request)#频率组件

self.initial详解

APIView中的dispatch的更多相关文章

  1. CBV中的dispatch

    之前介绍了FBV和CBV ,下面我们看一下CBV中的dispatch dispatch函数在类View中定义,作用就是通过反射查找get或post函数,所以在执行get或post函数之前,dispat ...

  2. GCD 中使用 dispatch group 进行同步操作

    话不多说,先上代码,在分析 Code - (void)viewDidLoad { [super viewDidLoad]; dispatch_group_t group1 = dispatch_gro ...

  3. 深入ObjC GCD中的dispatch group工作原理。

    本文是基于GCD的支持库libdispatch的源代码分析的结果或是用于作为源代码阅读的参考,尽量不帖代码,力求用UML图来说明工作流. 本文参考的源代码版本为v501.20.1,如有兴趣请自行到苹果 ...

  4. vuex中的dispatch和commit

    dispatch:含有异步操作,eg:向后台提交数据,写法: this.$store.dispatch('mutations方法名',值) commit:同步操作,写法:this.$store.com ...

  5. DRF中的APIView源码分析

    首先写一个简单的drf接口 from rest_framework.views import APIView from rest_framework.response import Response ...

  6. RestFramework——API基本实现及dispatch基本源码剖析

    基于Django实现 在使用RestFramework之前我们先用Django自己实现以下API. API完全可以有我们基于Django自己开发,原理是给出一个接口(URL),前端向URL发送请求以获 ...

  7. django Rest Framework----APIView 执行流程 APIView 源码分析

    在django—CBV源码分析中,我们是分析的from django.views import View下的执行流程,这篇博客我们介绍django Rest Framework下的APIView的源码 ...

  8. DRF(1) - REST、DRF(View源码解读、APIView源码解读)

    一.REST 1.什么是编程? 数据结构和算法的结合. 2.什么是REST? 首先回顾我们曾经做过的图书管理系统,我们是这样设计url的,如下: /books/ /get_all_books/ 访问所 ...

  9. REST、DRF(View源码解读、APIView源码解读)

    一 . REST            前言 1 . 编程 : 数据结构和算法的结合 .小程序如简单的计算器,我们输入初始数据,经过计算,得到最终的数据,这个过程中,初始数据和结果数据都是数据,而计算 ...

随机推荐

  1. swoole学习--图文直播和聊天室

    其实这个也没有什么好值得记录的,但是前面都记下来了,我也顺便说说吧: 1.为了方便,最好把http服务声明为超全局变量. 2.在一些地方里面,你声明的http超全局变量是用不了的,你只能用他自己内置的 ...

  2. How to check if directory exist using C++ and winAPI

    如果看文件夹是否存在,必须看返回值是不是 INVALID_FILE_ATTRIBUTES #include <windows.h> #include <string> bool ...

  3. 使用spring boot创建fat jar APP

    文章目录 介绍 build和run fat jar和 fat war 更多配置 介绍 在很久很很久以前,我们部署web程序的方式是怎么样的呢?配置好服务器,将自己写的应用程序打包成war包,扔进服务器 ...

  4. Scala教程之:面向对象的scala

    文章目录 面向对象的scala Unified Types Classes Traits 面向对象的scala 我们知道Scala是一种JVM语言,可以合java无缝衔接,这也就大大的扩展了scala ...

  5. material UI中withStyles和makeStyles的区别

      在material UI中,withStyles和makeStyles是经常使用的两个用于封装样式的函数.对于刚使用material UI的开发者而言,可能不太清楚这两者的区别.   本文简要探究 ...

  6. tomcat多个springboot项目启动失败

    多个springboot项目打包成war包并放到tomcat下运行时出错了错误信息: Caused by: org.springframework.jmx.export.UnableToRegiste ...

  7. D. Count the Arrays 计数题

    D. Count the Arrays 也是一个计数题. 题目大意: 要求构造一个满足题意的数列. \(n\) 代表数列的长度 数列元素的范围 \([1,m]\) 数列必须有且仅有一对相同的数 存在一 ...

  8. D - Pearls HDU - 1300 斜率dp+二分

    D - Pearls HDU - 1300 这个题目也是一个比较裸的斜率dp,依照之前可以推一下这个公式,这个很好推 这个注意题目已经按照价格升序排列序,所以还是前缀和还是单调的. sum[i] 表示 ...

  9. WCF(一)

    在学习WCF之前要知道几个术语: 一:终结点 终结点由3个要素组成:契约,绑定,地址. 1.契约:契约属于一个服务公开接口的一部分.一个服务的契约,定义了服务端公开的服务方法,使用的传输协议,可访问的 ...

  10. Matlab2016b线性规划函数linprog的几个问题

    一.如何设置算法为单纯型法: options = optimoptions('linprog','Algorithm','dual-simplex') 二.linprog的参数用法: [x,Fval, ...