首页 > 其他分享 >CBV加装饰器的三种方式

CBV加装饰器的三种方式

时间:2022-12-10 17:22:27浏览次数:40  
标签:get request method 三种 CBV login 装饰 decorator

CBV如何加装饰器

# 先导包:from django.utils.decorators import method_decorator
# 方式一,加在某个具体的方法上,格式:@method_decorator(装饰器名字)
# 方式二,加在类上,格式:@method_decorator(装饰器名字,name='给某个方法加')
# 方式三,写一个dispatch方法,加在其上,该类里的方法都会被装饰,格式:@method_decorator(login_auth)

CBV加装饰器实例

from django.shortcuts import render, HttpResponse, redirect
from django.views import View
from functools import wraps

# 给CBV加装饰器,需要导入以下模块
from django.utils.decorators import method_decorator

# Create your views here.
def login(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        pwd = request.POST.get('pwd')
        if username == 'jason' and pwd == '123':
            request.session['name'] = 'jason'
            return redirect('/home/')
    return render(request, 'login.html')


def login_auth(func):
    @wraps(func)
    def inner(request, *args, **kwargs):
        if request.session.get('name'):
            return func(request, *args, **kwargs)
        return redirect('/login/')
    return inner


# 第二种加装饰器的方法,@method_decorator(装饰器名字,name='给某个方法加')
# @method_decorator(login_auth, name='get')
class MyHome(View):
    # 第三种加装饰器的方法,该类里的方法都会被装饰
    @method_decorator(login_auth)
    def dispatch(self, request, *args, **kwargs):
        super().dispatch(request, *args, **kwargs)

    # 第一种加装饰器的方法,@method_decorator(装饰器名字)
    # @method_decorator(login_auth)
    def get(self, request):
        """
        处理get请求
        """
        return HttpResponse('get')

    def post(self, request):
        """
        处理post请求
        """
        return HttpResponse('post')

标签:get,request,method,三种,CBV,login,装饰,decorator
From: https://www.cnblogs.com/wfw001-2018/p/16971914.html

相关文章

  • Tomcate三种部署项目的方法
    在tomcat中三种部署项目的方法第一种方法:在tomcat中的conf目录中,在server.xml中的,<host/>节点中添加:<Contextpath="/hello"docBase="D:eclipse3.2.2forwebtoolsworkspace......
  • 实现ViewPager懒加载的三种方法
    在项目中ViewPager和Fragment接口框架已经是处处可见,但是在使用中,我们肯定不希望用户在当前页面时就在前后页面的数据,加入数据量很大,而用户又不愿意左右滑动浏览,那么这时候V......
  • 数据库,部分函数依赖,传递函数依赖,完全函数依赖,三种范式的区别
    要讲清楚范式,就先讲讲几个名词的含义吧:部分函数依赖:设X,Y是关系R的两个属性集合,存在X→Y,若X’是X的真子集,存在X’→Y,则称Y部分函数依赖于X。举个例子:学生基本信息表R中(学号,......
  • Linux系统常见的三种设备分类!
    Linux系统中设备主要分为哪几类?在Linux中,设备主要分为三种,分别是:块设备、字符设备和网络设备,接下来通过这篇文章详细介绍一下。Linux系统中的设备可以分为字符设备......
  • Python学习基础笔记十九——装饰器
    OverridetheentrypointofanimageIntroducedinGitLabandGitLabRunner9.4.Readmoreaboutthe extendedconfigurationoptions.Beforeexplainingtheav......
  • 三种过渡到IPv6的隧道模式
    隧道模式直接利用现有IPv4网络实现与IPv6网络的通信。1、6PE在核心网络是IPv4网络的情况下,如果要构建一个IPv6网络,可以通过在支持IPv6协议的边界路由器之间建立IPv6隧道,由这......
  • Redis的三种模式
    一、主从复制1.1主从复制的概念主从复制,是指将一台Redis服务器的数据,复制到其他的Redis服务器。前者称为主节点(Master),后者称为从节点(Slave);数据的复制是单向的,只......
  • 监控Kubernetes集群证书过期时间的三种方案
    前言Kubernetes中大量用到了证书,比如ca证书、以及kubelet、apiserver、proxy、etcd等组件,还有kubeconfig文件。如果证书过期,轻则无法登录Kubernetes集群,重则整......
  • dojo,jquery,mootools三种框架实现的ajax效果
    经常在微BLOG上,或者象tudou,ku6等视频网站上,看到"查看评论"的按钮,点后就显示列表,是AJAX效果的,找到老外的一篇文,讲这个实现:​​​http://davidw......
  • JUC6 中断机制与线程通信三种让线程等待和唤醒的方法:
    1.线程中断1.1什么是线程中断①.一个线程不应该由其他线程来强制中断或停止,而是应该由线程自己自行停止,所以,Thread.stop、Thread.suspend、Thread.resume都已经被废......