(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. MySQL的远程链接

    安装好我们的mMySQL,是不是也有种无从下手的感觉,不用怕,接下来我们可以使用远程连接来可视化我们的数据库的数据: 1. 打开我们的数据库,帐号是root,密码查看文件就行了 2.使用命令: mys ...

  2. 在Spring Boot使用H2内存数据库

    文章目录 添加依赖配置 数据库配置 添加初始数据 访问H2数据库 在Spring Boot使用H2内存数据库 在之前的文章中我们有提到在Spring Boot中使用H2内存数据库方便开发和测试.本文我 ...

  3. Springboot中,Tomcat启动war包的流程

    将一个SpringBoot项目,打成war包 <!-- 1. 修改POM依赖 --> <dependency> <groupId>org.springframewo ...

  4. 【Linux常见命令】ifconfig命令:配置与查看网络信息

    ifconfig(interfaces config).通常需要以root身份登录或使用sudo来使用ifconfig工具 ifconfig 命令用来查看和配置网络设备.当网络环境发生改变时可通过此命 ...

  5. Python3的日期和时间

    2019独角兽企业重金招聘Python工程师标准>>> python 中处理日期时间数据通常使用datetime和time库 因为这两个库中的一些功能有些重复,所以,首先我们来比较一 ...

  6. 谈谈JavaScript中的变量、指针和引用

    1.变量 我们可能产生这样一个疑问:编程语言中的变量到底是什么意思呢? 事实上,当我们定义了一个变量a时,就是在存储器中指定了一组存储单元,并将这组存储单元命名为a.变量a的值实际上描述的是这组存储单 ...

  7. 一句话教你分清楚UML组合聚合和联系!

    组合:组合后的实体消失,则所有构成实体的部件都无意义,可以理解为不能独立存在 定义: 与聚合相比,组合描述的是这样的关联关系,部分离开整体后就没有实际意义了.所以我们说组合是一种很强的关联关系. 例子 ...

  8. 基于TCP的客户端和服务器端的代码设计

    实验平台 linux 实验内容 编写TCP服务器和客户端程序,程序运行时服务器等待客户端连接.一旦连接成功,服务器显示客户端的IP地址和端口号,并向客户端发送字符串 实验原理 TCP是面向连接的通信, ...

  9. H - Food HDU - 4292 网络流

    题目   You, a part-time dining service worker in your college’s dining hall, are now confused with a n ...

  10. Zabbix 添加vmware esxi监控

    1) Import the provided template. - TEMPLATE.VMWARE_ESXi_6.0_CIM.xml 2) Install Dependencies: # yum - ...