备忘

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的装饰器例子的更多相关文章

  1. diango中的MTV——FBV/CBV以及装饰器的复用问题解决

    MVC M: model 模型 与数据库交互 V: view 视图 HTML C:controller 控制器 流程 和 业务逻辑 MTV M:model ORM T:template 模板 HTML ...

  2. Django CBV加装饰器、Django中间件、auth模块

    一. CBV加装饰器 在视图层中,基于函数的视图叫FBV(function base views),基于类的视图叫CBV(class base views).当需要用到装饰器时,例如之前的基于Cook ...

  3. django CBV 及其装饰器

    #urls.py from django.contrib import admin from django.urls import path, re_path from app01 import vi ...

  4. CBV加装饰器解决登录注册问题和 <<中间件>>

    文本目录 CBV加装饰器解决登录注册问题 一:什么是中间件 二:中间件有什么用 三:自定义中间件 四:中间件应用场景 五:SCRF TOKEN跨站请求伪造 六: 其他操作 CBV加装饰器解决登录注册问 ...

  5. Django视图函数函数之视图装饰器

    FBV模式装饰器: 普通函数的装饰器(语法糖@) views.py from django.shortcuts import render def wrapper(f): def inner(*arg ...

  6. Cookie与Session、CBV添加装饰器

    cookie Cookie的由来 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响,也不 ...

  7. 浅谈Django的中间件与Python的装饰器

    浅谈Django的中间件 与Python的装饰器 一.原理 1.装饰器是Python的一种语法应用,利用闭包的原理去更改一个函数的功能,即让一个函数执行之前先到另外一个函数中执行其他需求语句,在执行该 ...

  8. Django视图函数:CBV与FBV (ps:补充装饰器)

    CBV 基于类的视图  FBV 基于函数的视图 CBV: 1 项目目录下: 2 urlpatterns = [ 3 path('login1/',views.Login.as_view()) #.as ...

  9. 基于Django-Cookie的CBV和FBV的用户验证装饰器

    FBV模式 def cookie(func):       def deco(request,*args,**kwargs):             u = request.get_signed_c ...

随机推荐

  1. com.netflix.zuul.exception.ZuulException: Forwarding error

    一.问题描述 在使用Spring Cloud的zuul组件,做路由转发时,每次重新启动后端服务,头几次调用都会出现com.netflix.zuul.exception.ZuulException: F ...

  2. yarn基础解释

    https://yarnpkg.com/zh-Hans/docs Yarn 对你的代码来说是一个包管理器, 你可以通过它使用全世界开发者的代码,或者分享自己的代码. 代码通过包(package)(或者 ...

  3. 【Oracle】【4】mybatis insert/update 数据后返回关键字段

    1,插入 insert 场景:ID字段的值是数据库表“默认/表达式”(sys_guid())自动生成,插入一条数据到数据库后,需要获取该条数据的ID 解决方案: (1)Service层生成UUID p ...

  4. 基于jquery实现页面loading加载效果

    实现loading 加载提示 ······ 透明遮罩 居中效果 具体代码如下: CSS样式部分: <style type="text/css"> .background ...

  5. oracle查看列数据类型

    select * from user_tab_columns where table_name = upper('表名')

  6. Docker的简单介绍及使用

    Docker介绍 Docker是Docker.Inc公司开源的一个基于LXC技术之上构建的Container容器引擎,源代码托管在GitHub上,基于Go语言并遵从Apache2.0协议开源. Doc ...

  7. 依赖注入 IOC

    首先,明确下IoC是什么东西: 控制反转(Inversion of Control,英文缩写为IoC)是一个重要的面向对象编程的法则来削减计算机程序的耦合问题,也是轻量级的Spring框架的核心. 控 ...

  8. Redis的JAVA连接

    ShardedJedis用法 package com.zhi.demo; import java.util.Arrays; import java.util.List; import redis.cl ...

  9. js之单例

    所谓单例,指的是只有一个实例的对象. js通过对象字面量的方式来创建单例对象. var sig = { name:value, method:function(){ } }

  10. python 利用turtle库绘制五角星

    # -*- coding: utf-8 –*-import turtleimport math def draw_polygon(aTurtle, size=50, n=3): for i in ra ...