关于装饰器

示例:

有返回值的装饰器:判断用户是否登录,如果登录继续执行函数,否则跳回登录界面

def auth(func):
def inner(request, *args, **kwargs):
username = request.COOKIES.get('username')
if not username:
# 如果无法获取 'username' COOKIES,就跳转到 '/login.html'
return redirect('/login.html')
# 原函数执行前
res = func(request, *args, **kwargs)
# 原函数执行后
return res
return inner

FBV:

直接在需要装饰到函数上面加上@auth

@auth
def index(request):
return render(request, 'index.html')

CBV:

关于 CBV

只需要给部分方法加上装饰器
from django import views
from django.utils.decorators import method_decorator class Order(views.View): @method_decorator(auth)
def get(self,reqeust):
v = reqeust.COOKIES.get('username')
return render(reqeust,'index.html',{'current_user': v}) def post(self,reqeust):
v = reqeust.COOKIES.get('username')
return render(reqeust,'index.html',{'current_user': v})
需要给所有方法加上装饰器

通过 dispatch 实现

from django import views
from django.utils.decorators import method_decorator class Order(views.View): @method_decorator(auth)
def dispatch(self, request, *args, **kwargs):
return super(Order,self).dispatch(request, *args, **kwargs) def get(self,reqeust):
v = reqeust.COOKIES.get('username')
return render(reqeust,'index.html',{'current_user': v}) def post(self,reqeust):
v = reqeust.COOKIES.get('username')
return render(reqeust,'index.html',{'current_user': v})

直接在类上给 dispatch 添加装饰器

from django import views
from django.utils.decorators import method_decorator @method_decorator(auth,name='dispatch')
class Order(views.View): def get(self,reqeust):
v = reqeust.COOKIES.get('username')
return render(reqeust,'index.html',{'current_user': v}) def post(self,reqeust):
v = reqeust.COOKIES.get('username')
return render(reqeust,'index.html',{'current_user': v})

Django views 中的装饰器的更多相关文章

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

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

  2. django类视图的装饰器验证

    django类视图的装饰器验证 django类视图的get和post方法是由View内部调用dispatch方法来分发,最后调用as_view来完成一个视图的流程. 函数视图可以直接使用对应的装饰器 ...

  3. 简单说明Python中的装饰器的用法

    简单说明Python中的装饰器的用法 这篇文章主要简单说明了Python中的装饰器的用法,装饰器在Python的进阶学习中非常重要,示例代码基于Python2.x,需要的朋友可以参考下   装饰器对与 ...

  4. Typescript中的装饰器原理

    Typescript中的装饰器原理 1.小原理 因为react中的高阶组件本质上是个高阶函数的调用, 所以高阶组件的使用,我们既可以使用函数式方法调用,也可以使用装饰器. 也就是说,装饰器的本质就是一 ...

  5. Python 标准库中的装饰器

    题目描述 1.简单举例 Python 标准库中的装饰器 2.说说你用过的 Python 标准库中的装饰器 1. 首先,我们比较熟悉,也是比较常用的 Python 标准库提供的装饰器有:property ...

  6. 【Python】python中的装饰器——@

    对装饰器本来就一知半解的,今天终于弄清楚了,Python中的装饰器是对装饰者模式的很好运用,简化到骨子里了. python中为什么需要装饰器,看这里:http://www.cnblogs.com/hu ...

  7. Python 中实现装饰器时使用 @functools.wraps 的理由

    Python 中使用装饰器对在运行期对函数进行一些外部功能的扩展.但是在使用过程中,由于装饰器的加入导致解释器认为函数本身发生了改变,在某些情况下——比如测试时——会导致一些问题.Python 通过  ...

  8. 写python中的装饰器

    python中的装饰器主要用于在已有函数实现功能前附加需要输出的信息,下面将用实例展示我如何写装饰器. 首先分别尝试写装饰器装饰一个无参函数和一个有参函数(被装饰函数仅输出,无返回值情况下) def ...

  9. python中的装饰器decorator

    python中的装饰器 装饰器是为了解决以下描述的问题而产生的方法 我们在已有的函数代码的基础上,想要动态的为这个函数增加功能而又不改变原函数的代码 例如有三个函数: def f1(x): retur ...

随机推荐

  1. 算法问题实战策略 FENCE

    地址 https://algospot.com/judge/problem/read/FENCE 开始考虑暴力遍历 #include <iostream> #include <str ...

  2. 洛谷 SP14932 LCA - Lowest Common Ancestor

    洛谷 SP14932 LCA - Lowest Common Ancestor 洛谷评测传送门 题目描述 A tree is an undirected graph in which any two ...

  3. P4287 [SHOI2011]双倍回文

    题意 考虑对每个节点\(x\)维护\(lastpos_x\)表示\(x\)的所有后缀回文串中第一个\(len\leqslant len_x/2\)并且能和\(x\)最后一个字符匹配的,之后枚举节点,判 ...

  4. 【CodeChef】December Challenge 2019 Div1 解题报告

    点此进入比赛 这次比赛本来想好好打的,但不幸的是,这周先是要认真复习准备月考,考完又是发烧在床上躺了一个周末,所以最终没能打完. 我还是好弱啊. \(T1\):Binary XOR(点此看题面) 大致 ...

  5. LeetCode 599: 两个列表的最小索引总和 Minimum Index Sum of Two Lists

    题目: 假设 Andy 和 Doris 想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示. Suppose Andy and Doris want to cho ...

  6. Linux进程和计划任务实践

    1.显示统计占用系统内存最多的进程,并排序. 方法一 [root@test ~]#ps aux --sort=%mem USER PID %CPU %MEM VSZ RSS TTY STAT STAR ...

  7. mysql压缩备份导入导出

    mysqldump工具自带选项没有对导出备份文件压缩功能,可结合gzip只使用一条命令压缩导出文件,方法如下: mysqldump压缩导出:# mysqldump -h192.168.0.3 -P33 ...

  8. 3、Hibernate的多表关联

    一.数据库中的表关系: 一对一关系 一个人对应一张身份证,一张身份证对应一个人,一对一关系是最好理解的一种关系,在数据库建表的时候可以将人表的主键放置与身份证表里面,也可以将身份证表的主键放置于人表里 ...

  9. 解决:perl: warning: Please check that your locale settings

    问题: perl: warning: Setting locale failed. perl: warning: Please check that your locale settings: LAN ...

  10. C#函数的参数传递2(ref\out)

    using System; namespace class1 { class program { static void Main(string[] args) { Console.Write(&qu ...