关于装饰器

示例:

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

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. [LeetCode] 42. Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  2. app——升级测试点

    APP版本升级的测试点 该文章转载于:https://www.cnblogs.com/changpuyi/p/8618755.html   移动端版本更新升级是一个比较重要的功能点,主要分为强制更新和 ...

  3. EJB组件开发实记(1)

    安装JBoss或者Wildfly jdk1.4以上. Eclipes安装插件 JBoss Tools: eclipes Jee photon 在eclipes 内部点击 >>Windows ...

  4. 安装 Java

    1.rpm下载地址 https://download.oracle.com/otn/java/jdk/7u79-b15/jdk-7u79-linux-x64.rpm?AuthParam=1570520 ...

  5. Dubbo+Zookeeper实现简单的远程方法调用示例

    1. Dubbo介绍 示例代码:Github 1.1 RPC Remote Procedure Call:远程过程调用 1.2 Dubbo架构 Subscribe 订阅:签署:赞成 Monitor 监 ...

  6. Git 实用命令记录

    自从上次写了一篇 Git 入门 的相关博客以来,一直自以为自己能完全的掌握 Git,其实不然,今天一小伙问我,如何删除远程上面的一个分支,呃,不会. git branch -d 分支名 只能删除本地的 ...

  7. C# regular expression to validate email

    using System; using System.Text.RegularExpressions; namespace ConsoleApp375 { class Program { static ...

  8. python基础(27):类成员的修饰符、类的特殊成员

    1. 类成员的修饰符 类的所有成员在上一步骤中已经做了详细的介绍,对于每一个类的成员而言都有两种形式: 公有成员,在任何地方都能访问 私有成员,只有在类的内部才能方法 私有成员和公有成员的定义不同:私 ...

  9. liunx简单命令

    mysql -h主机地址 -u用户名 -p用户密码 --进入数据库1.显示数据库列表. show databases; 2.显示库中的数据表: use mysql: //打开库 show tables ...

  10. javascript常用数据验证函数

    正则表达式日期验证函数 function CheckDate(str){       //在JavaScript中,正则表达式只能使用"/"开头和结束,不能使用双引号       ...