Django自定义装饰器
装饰器模板:
def decorator(func):
def wrapper(*args,**kwargs):
return func(*args,**kwargs)
return wrapper
装饰器应用:
def log_in(func):
'''身份认证装饰器,
:param func:
:return:
'''
def wrapper(request,*args,**kwargs):
if not request.session.get("is_login"):
return redirect("/book/login/")
return func(request,*args, **kwargs)
return wrapper @log_in #author=log_in(author)
def author(request):
author_list = models.Author.objects.all()
return render(request, "author.html", {"authorList": author_list})
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.views.generic import TemplateView class ProtectedView(TemplateView):
template_name = 'secret.html' @method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(ProtectedView, self).dispatch(*args, **kwargs)
Django自定义装饰器的更多相关文章
- python django 自定义 装饰器
# -*-coding:utf-8-*- __author__ = "GILANG (pleasurelong@foxmail.com)" """ d ...
- Django学习笔记第八篇--实战练习四--为你的视图函数自定义装饰器
零.背景: 对于登录后面所有视图函数,都需要验证登录信息,一般而言就是验证cookie里面的一些信息.所以你可以这么写函数: def personinfo(request): ": retu ...
- Django - CBV装饰器实现用户登录验证
一.使用Django自带的decorator 通常情况,使用 函数定义的view,可以直接使用 login_required 直接装饰 @login_required def index(reques ...
- 详解Django中六个常用的自定义装饰器
装饰器作用 decorator是当今最流行的设计模式之一,很多使用它的人并不知道它是一种设计模式.这种模式有什么特别之处? 有兴趣可以看看Python Wiki上例子,使用它可以很方便地修改对象行为, ...
- Django中六个常用的自定义装饰器
装饰器作用 decorator是当今最流行的设计模式之一,很多使用它的人并不知道它是一种设计模式.这种模式有什么特别之处? 有兴趣可以看看Python Wiki上例子,使用它可以很方便地修改对象行为, ...
- django CBV装饰器 自定义django中间件 csrf跨站请求伪造 auth认证模块
CBV加装饰器 第一种 @method_decorator(装饰器) 加在get上 第二种 @method_decorator(login_auth,name='get') 加在类上 第三种 @met ...
- Django CBV装饰器 中间件 auth模块 CSRF跨站请求
CBV添加装饰器 给CBV添加装饰器有三种方法,三种方法都需要导入模块: from django.utils.decorators import method_decorator 第一种直接在方法上面 ...
- django添加装饰器
引入模块: from django.utils.decorators import method_decorator 添加:@method_decorator(func) from django.ut ...
- django view 装饰器
Django提供了几个可以应用于视图以支持各种HTTP特性的装饰器 Allowed HTTP django.views.decorators.http里的装饰器可以根据请求方法限制对视图的访问. re ...
随机推荐
- 【Python】-NO.96.Note.2.Python -【Python 基础】
1.0.0 Summary Tittle:[Python]-NO.95.Note.1.Python -[Python 老男孩 基础]- Style:Python Series:Python Since ...
- 对接口(interface)的思考
接口,java中用关键字interface定义.今天学习java基础API时,发现一个特点:每个包中都定义了一堆的接口,感觉有马克思主义中提到的“上层建筑”的味道.接口就是为需要实现的功能定一个基调, ...
- network error:software caused connection abort
使用Putty链接阿里云香港服务器报这个错误. vim /etc/ssh/sshd_config 找到如下配置 #ClientAliveInterval 540 #ClientAliveCountMa ...
- Hybrid设计--如何落地一个Hybrid项目
前后分离 -> 统一前端框架 -> 同一个账号体系 -> 登录注册的公共页 -> 有了这些公共业务后 推行 -> Hybrid 技术 底层容器开发出来后 -&g ...
- Serveral effective linux commands
1. 统计当前文件夹下文件个数(不包括子目录下文件): $ ls -l | grep "^-" | wc -l 2. 统计当前文件夹下文件个数(包括子目录下文件): $ ls -l ...
- bat cmd 获取管理员权限
@ echo off % % ver|find "5.">nul&&goto :Admin mshta vbscript:createobject()(win ...
- Oracle的下载安装教程以及所出现的问题
1.下载地址 64位 https://www.oracle.com/technetwork/database/enterprise-edition/downloads/112010-win64soft ...
- c# 类一般在哪里实例化,是在类内、方法内还是其他地方?
根据情况,你要一个页面内全局的就在类与方法之间实例化,如果一个方法需要使用这个类的对象,就在内部实例化
- IIS转发需要的模块
1.Application Request Routing https://www.iis.net/downloads/microsoft/application-request-routing 2 ...
- Lua之table
Lua table(表) 参考:http://www.runoob.com/lua/lua-tables.html table 是 Lua 的一种数据结构用来帮助我们创建不同的数据类型,如:数字.字典 ...