之前介绍了FBV和CBV ,下面我们看一下CBV中的dispatch

dispatch函数在类View中定义,作用就是通过反射查找get或post函数,所以在执行get或post函数之前,dispatch函数是肯定会被执行的。因此我们可以通过super,来重写dispatch,达到一个类似装饰器的功能。
views.py
from django.shortcuts import render
from django.views import View class Index(View): def dispatch(self, request, *args, **kwargs):
print('Before')
ret = super(Index, self).dispatch(request, *args, **kwargs)
print('After')
return ret def get(self, req):
print('method is :' + req.method)
return render(req, 'index.html') def post(self, req):
print('method is :' + req.method)
return render(req, 'index.html')

  

后台输出:

Before
method is :GET
After
Before
method is :POST
After

可见,我们可以在执行get或者post函数时,通过dispatch函数做一些自己想做的事情。

CBV中的dispatch的更多相关文章

  1. python框架之Django(8)-CBV中添加装饰器

    现有如下检查登录装饰器: from functools import wraps def check_login(func): @wraps(func) def inner(request, *arg ...

  2. django ----CBV中加装饰器

    CBV中加装饰器 from django import views from django.utils.decorators import method_decorator def login_aut ...

  3. Python - Django - 在 CBV 中使用装饰器

    urls.py: from django.conf.urls import url from app02 import views urlpatterns = [ # app02 url(r'^app ...

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

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

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

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

  6. APIView中的dispatch

    (1)dispatch方法详解----封装原有的request对象 (原request中的方法和属性均可直接在封装后的request中调用,或者使用request._request也可,如:reque ...

  7. vuex中的dispatch和commit

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

  8. 如何在CBV中使用装饰器

    要区分函数装饰器和方法装饰器得区别 ,方法装饰器得第一个参数是self本身,所以函数装饰器不能用

  9. Django REST framework中的版本控制

    1.REST framework版本控制的流程分析 1.1 determine_version方法的执行流程 首先,请求到达REST framework的CBV,执行CBV中的dispatch方法再次 ...

随机推荐

  1. 【scala】集合框架

  2. 【lightoj-1094】树的直径(DFS)

    链接:http://www.lightoj.com/volume_showproblem.php?problem=1094 题意: 一共n各节点编号0-n-1, 输入n-1条无向边代表u-v距离为w, ...

  3. Application的作用

    Application可实现数据共享 例如: 一.新建一个空的工程,并新建一个App类,继承自Application public class App extends Application { pr ...

  4. 前端 velocity(.vm)模板里写ajax出现解析异常

    异常信息:Caused by: org.apache.velocity.exception.ParseErrorException: Encountered "{" at dist ...

  5. JavaScript JSON.parse()和JSON.stringify()

    parse用于从一个字符串中解析出json对象,如 var str = '{"name":"huangxiaojian","age":&qu ...

  6. Flask 的路由系统 FBV 与 CBV

    Flask的路由系统 本质: 带参数的装饰器 传递函数后 执行 add_url_rule 方法 将 函数 和 url 封装到一个 Rule对象 将Rule对象 添加到 app.url_map(Map对 ...

  7. css 中 overflow: hidden清楚浮动的真正原因

    1. 先上一段代码清楚浮动的代码, 外层ul设置overflow为hidden, 内层li设置float为left左浮动 <!DOCTYPE html> <html> < ...

  8. Intellij IDEA带参数启动Springboot注意事项

    问题 不同版本的spring-boot-maven-plugin的jvm参数配置有所不同,同时与通过main方法启动springboot程序传递参数也有所不同. 分析 在运行main方法时,可以通过j ...

  9. Electron 打包时下载 xxx-electron-v1.6.8--x64.zip 文件出错

    Electron 打包时下载 xxx-electron-v1.6.8--x64.zip 文件出错 今天在windows上打包其它平台的Electron应用的时候,由于是第一次,所以总是下载 xxx-e ...

  10. 设计模式之桥接(bridge)模式

    在现实生活中,我们常常会用到两种或多种类型的笔,比如毛笔和蜡笔.假设我们需要大.中.小三种类型的画笔来绘制12中不同的颜色,如果我们使用蜡笔,需要准备3*12=36支.但如果使用毛笔的话,只需要提供3 ...