django的FBV和CBV的装饰器例子
备忘
def auth(func):
def inner(request,*args,**kwargs):
u = request.COOKIES.get('username111')
if not u:
return redirect('/login/')
print("call index()")
return func(request,*args,**kwargs)
return inner #FBV
@auth
def index(request): #带签名的cookie
obj = HttpResponse('')
obj.set_signed_cookie('username','',salt='adasaa')#salt加盐,设置cookie,加密
request.get_signed_cookie('username','',salt='adasaa')#获取cookie u = request.COOKIES.get('username111')
if not u:
return redirect('/login/')
else:
return render(request,'index.html',{'u':u}) #CBV
from django.views import View
from django.utils.decorators import method_decorator
class Order(View): @method_decorator(auth)
def get(self,request):
u = request.COOKIES.get('username111')
return render(request, 'index.html', {'u': u}) def post(self,request):
u = request.COOKIES.get('username111')
if not u:
return redirect('/login/')
else:
return render(request, 'index.html', {'u': u})
CBV如果有很多方法,都要加装饰器的话,可以加在dispatch方法上,这样类里面的方法就不用再一一加上装饰了
def auth(func):
def inner(request,*args,**kwargs):
u = request.COOKIES.get('username111')
if not u:
print("未登陆")
return redirect('/login/')
print("call now")
return func(request,*args,**kwargs)
return inner #FBV
@auth
def index(request): #带签名的cookie
obj = HttpResponse('')
obj.set_signed_cookie('username','',salt='adasaa')#salt加盐,设置cookie,加密
request.get_signed_cookie('username','',salt='adasaa')#获取cookie u = request.COOKIES.get('username111')
if not u:
return redirect('/login/')
else:
return render(request,'index.html',{'u':u}) #CBV
from django.views import View
from django.utils.decorators import method_decorator
class Order(View): @method_decorator(auth)
def dispatch(self, request, *args, **kwargs):
return super(self,Order).dispatch(request, *args, **kwargs) def get(self,request):
u = request.COOKIES.get('username111')
return render(request, 'index.html', {'u': u}) def post(self,request):
u = request.COOKIES.get('username111')
if not u:
return redirect('/login/')
else:
return render(request, 'index.html', {'u': u})
再来一个终极版的,不用重写dispatch,直接加在类上面:
def auth(func):
def inner(request,*args,**kwargs):
u = request.COOKIES.get('username111')
if not u:
print("未登陆")
return redirect('/login/')
print("call now")
return func(request,*args,**kwargs)
return inner #FBV
@auth
def index(request): #带签名的cookie
obj = HttpResponse('')
obj.set_signed_cookie('username','',salt='adasaa')#salt加盐,设置cookie,加密
request.get_signed_cookie('username','',salt='adasaa')#获取cookie u = request.COOKIES.get('username111')
if not u:
return redirect('/login/')
else:
return render(request,'index.html',{'u':u}) #CBV
from django.views import View
from django.utils.decorators import method_decorator @method_decorator(auth,name='dispatch')
class Order(View): # @method_decorator(auth)
# def dispatch(self, request, *args, **kwargs):
# return super(self,Order).dispatch(request, *args, **kwargs) def get(self,request):
u = request.COOKIES.get('username111')
return render(request, 'index.html', {'u': u}) def post(self,request):
u = request.COOKIES.get('username111')
if not u:
return redirect('/login/')
else:
return render(request, 'index.html', {'u': u})
django的FBV和CBV的装饰器例子的更多相关文章
- diango中的MTV——FBV/CBV以及装饰器的复用问题解决
MVC M: model 模型 与数据库交互 V: view 视图 HTML C:controller 控制器 流程 和 业务逻辑 MTV M:model ORM T:template 模板 HTML ...
- Django CBV加装饰器、Django中间件、auth模块
一. CBV加装饰器 在视图层中,基于函数的视图叫FBV(function base views),基于类的视图叫CBV(class base views).当需要用到装饰器时,例如之前的基于Cook ...
- django CBV 及其装饰器
#urls.py from django.contrib import admin from django.urls import path, re_path from app01 import vi ...
- CBV加装饰器解决登录注册问题和 <<中间件>>
文本目录 CBV加装饰器解决登录注册问题 一:什么是中间件 二:中间件有什么用 三:自定义中间件 四:中间件应用场景 五:SCRF TOKEN跨站请求伪造 六: 其他操作 CBV加装饰器解决登录注册问 ...
- Django视图函数函数之视图装饰器
FBV模式装饰器: 普通函数的装饰器(语法糖@) views.py from django.shortcuts import render def wrapper(f): def inner(*arg ...
- Cookie与Session、CBV添加装饰器
cookie Cookie的由来 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响,也不 ...
- 浅谈Django的中间件与Python的装饰器
浅谈Django的中间件 与Python的装饰器 一.原理 1.装饰器是Python的一种语法应用,利用闭包的原理去更改一个函数的功能,即让一个函数执行之前先到另外一个函数中执行其他需求语句,在执行该 ...
- Django视图函数:CBV与FBV (ps:补充装饰器)
CBV 基于类的视图 FBV 基于函数的视图 CBV: 1 项目目录下: 2 urlpatterns = [ 3 path('login1/',views.Login.as_view()) #.as ...
- 基于Django-Cookie的CBV和FBV的用户验证装饰器
FBV模式 def cookie(func): def deco(request,*args,**kwargs): u = request.get_signed_c ...
随机推荐
- synchronized相关用法简述
synchronized 锁,他是一个java 的关键字,能够保证同一线程只有一个线程访问或使用此修饰的代码块 用法 synchronized方法,synchronized块 synchronized ...
- PHP工厂模式计算面积与周长
<?phpinterface InterfaceShape{ function getArea(); function getCircumference();} /** * 矩形 */class ...
- php字符串的拆分截取
/* * strstr区分大小写 * stristr不区分大小写 * */ $str="test/abc.jpg"; echo stristr($str,'.'); echo '& ...
- python记录_day16 类的成员
一.变量 1.实例变量(又叫字段.属性) 创建对象时给对象赋值 形式: self.xxx = xxx 访问: 对象名.xxx 只能由对象访问 class Person: def __init_ ...
- java骰子求和算法
//扔 n 个骰子,向上面的数字之和为 S.给定 Given n,请列出所有可能的 S 值及其相应的概率public class Solution { /** * @param n an intege ...
- vs 2017 Integrated Security 为sspi 含义
关于sql连接语句中的Integrated Security=SSPI解决方法:即:Security Support Provider Interface设置Integrated Security为 ...
- python中的ConfigParser模块
1.简介 我们经常需要使用配置文件,例如.conf和.ini等类型,使用ConfigPaser模块可以对配置文件进行操作. 2.示例 现有配置文件test.ini,其内容如下: [section_a] ...
- [codechef July Challenge 2017] Chef and Sign Sequences
CHEFSIGN: 大厨与符号序列题目描述大厨昨天捡到了一个奇怪的字符串 s,这是一个仅包含‘<’.‘=’和‘>’三种比较符号的字符串.记字符串长度为 N,大厨想要在字符串的开头.结尾,和 ...
- 2015-09-22 css2
6.块元素和行内元素 1. 块元素特点:默认显示在父标签的左上角 块级元素默认占满一行(占满整个文档流) 常见的块元素:p,h1--h6,ul li, ol li,div,hr,table. 2.行内 ...
- Mysql优化要点
优化MySQL Mysql优化要点 慢查询 Explain mysql慢查询 注意事项 SELECT语句务必指明字段名称 SELECT *增加很多不必要的消耗(cpu.io.内存.网络带宽):增加了使 ...