CBV中的dispatch
之前介绍了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的更多相关文章
- python框架之Django(8)-CBV中添加装饰器
现有如下检查登录装饰器: from functools import wraps def check_login(func): @wraps(func) def inner(request, *arg ...
- django ----CBV中加装饰器
CBV中加装饰器 from django import views from django.utils.decorators import method_decorator def login_aut ...
- Python - Django - 在 CBV 中使用装饰器
urls.py: from django.conf.urls import url from app02 import views urlpatterns = [ # app02 url(r'^app ...
- GCD 中使用 dispatch group 进行同步操作
话不多说,先上代码,在分析 Code - (void)viewDidLoad { [super viewDidLoad]; dispatch_group_t group1 = dispatch_gro ...
- 深入ObjC GCD中的dispatch group工作原理。
本文是基于GCD的支持库libdispatch的源代码分析的结果或是用于作为源代码阅读的参考,尽量不帖代码,力求用UML图来说明工作流. 本文参考的源代码版本为v501.20.1,如有兴趣请自行到苹果 ...
- APIView中的dispatch
(1)dispatch方法详解----封装原有的request对象 (原request中的方法和属性均可直接在封装后的request中调用,或者使用request._request也可,如:reque ...
- vuex中的dispatch和commit
dispatch:含有异步操作,eg:向后台提交数据,写法: this.$store.dispatch('mutations方法名',值) commit:同步操作,写法:this.$store.com ...
- 如何在CBV中使用装饰器
要区分函数装饰器和方法装饰器得区别 ,方法装饰器得第一个参数是self本身,所以函数装饰器不能用
- Django REST framework中的版本控制
1.REST framework版本控制的流程分析 1.1 determine_version方法的执行流程 首先,请求到达REST framework的CBV,执行CBV中的dispatch方法再次 ...
随机推荐
- winform 中 MessageBox 用法大全
(转自:http://blog.csdn.net/xuenzhen123/article/details/4808005) MessageBox.Show()共有21中重载方法.现将其常见用法总结如下 ...
- LeetCode OJ:Valid Anagram(有效字谜问题)
Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...
- poi自定义颜色设置(转)
原文链接 http://javapolo.iteye.com/blog/1604501 最近在项目的开发中使用到了apache poi,该组件可以让我们方便的操作excell,该工具非常容易上手,但 ...
- 2017.11.17 Demo-stm8+temperature timeing control
1Find the lab and add in project. Downtown it from ST official website..compile it to ensure it pa ...
- PostgreSQL恢复误操作
根据德歌的博客自己实践一下: 找到要恢复的前一个事物ID,然后回滚到那个时候: 使用pg_xlogdump分析XLOG -bash-4.1$ pg_xlogdump 00000002000000000 ...
- 题目一:给出一个n,代表有从1到n的数字[1,2,3,··· n],问可以构成多少种二叉搜索树?
题目一:给出一个n,代表有从1到n的数字[1,2,3,··· n],问可以构成多少种二叉搜索树? 一开始的想法是直接递归构造,时间复杂度是指数上升:后来想法是找规律:先看例子: n = 1, 有一个元 ...
- Mybatis为实体类定义别名typeAliases
以新增一个用户为例子,原UserMapper.xml配置如下: <insert id="addUser" parameterType="main.User" ...
- HDU - 6433: H. Pow (简答题,输出大数)
There are n numbers 3^0, 3^1, . . . , 3^n-1. Each time you can choose a subset of them (may be empty ...
- OneNote如何使用
自从安装了Office2013后发现office套件中有很多的好东西,今天要和大家分享的就是Office套件中的OneNote软件,这狂软件能够很方便的记录我们生活中的一些学习资料.一些决绝方法的经验 ...
- bzoj 1226 学校食堂Dining
Written with StackEdit. Description 小\(F\) 的学校在城市的一个偏僻角落,所有学生都只好在学校吃饭.学校有一个食堂,虽然简陋,但食堂大厨总能做出让同学们满意的菜 ...