现有如下检查登录装饰器:

 from functools import wraps

 def check_login(func):
@wraps(func)
def inner(request, *args, **kwargs):
next_url = request.get_full_path()
if request.session.get("user"):
return func(request, *args, **kwargs)
else:
return redirect("/login/?next={}".format(next_url))
return inner

Code

使用

要在CBV视图中使用我们上面的check_login装饰器,有以下三种方式:

加在的get或post方法上

 from django.utils.decorators import method_decorator

 class HomeView(View):

     def dispatch(self, request, *args, **kwargs):
return super(HomeView, self).dispatch(request, *args, **kwargs) def get(self, request):
return render(request, "home.html") @method_decorator(check_login)
def post(self, request):
print("Home View POST method...")
return redirect("/index/")

Code

加在dispatch方法上

 from django.utils.decorators import method_decorator

 class HomeView(View):

     @method_decorator(check_login)
def dispatch(self, request, *args, **kwargs):
return super(HomeView, self).dispatch(request, *args, **kwargs) def get(self, request):
return render(request, "home.html") def post(self, request):
print("Home View POST method...")
return redirect("/index/")

Code

因为CBV中首先执行的就是dispatch方法,所以这么写相当于给get和post方法都加上了登录校验。

加在视图类上

直接加在视图类上,但method_decorator必须传name关键字参数。如果get方法和post方法都需要登录校验的话就写两个装饰器。

 from django.utils.decorators import method_decorator

 @method_decorator(check_login, name="get")
@method_decorator(check_login, name="post")
class HomeView(View): def dispatch(self, request, *args, **kwargs):
return super(HomeView, self).dispatch(request, *args, **kwargs) def get(self, request):
return render(request, "home.html") def post(self, request):
print("Home View POST method...")
return redirect("/index/")

Code

补充

CSRF Token相关装饰器

  • csrf_protect

    为当前函数强制设置防跨站请求伪造功能,即便settings中没有设置全局中间件。

  • csrf_exempt

    取消当前函数防跨站请求伪造功能,即便settings中设置了全局中间件。

  • 使用

    CSRF Token相关装饰器在CBV只能加到dispatch方法上,或者加在视图类上然后name参数指定为dispatch方法。

     from django.views.decorators.csrf import csrf_exempt, csrf_protect
    from django.utils.decorators import method_decorator class HomeView(View): @method_decorator(csrf_exempt)
    def dispatch(self, request, *args, **kwargs):
    return super(HomeView, self).dispatch(request, *args, **kwargs) def get(self, request):
    return render(request, "home.html") def post(self, request):
    print("Home View POST method...")
    return redirect("/index/")

    加到dispatch方法上

     from django.views.decorators.csrf import csrf_exempt, csrf_protect
    from django.utils.decorators import method_decorator @method_decorator(csrf_exempt, name='dispatch')
    class HomeView(View): def dispatch(self, request, *args, **kwargs):
    return super(HomeView, self).dispatch(request, *args, **kwargs) def get(self, request):
    return render(request, "home.html") def post(self, request):
    print("Home View POST method...")
    return redirect("/index/")

    加在视图类上

python框架之Django(8)-CBV中添加装饰器的更多相关文章

  1. Python - Django - 在 CBV 中使用装饰器

    urls.py: from django.conf.urls import url from app02 import views urlpatterns = [ # app02 url(r'^app ...

  2. django ----CBV中加装饰器

    CBV中加装饰器 from django import views from django.utils.decorators import method_decorator def login_aut ...

  3. django视图函数中 应用装饰器

    from django.shortcuts import render, redirect, HttpResponse from .forms import LoginForm, Registrati ...

  4. 如何在CBV中使用装饰器

    要区分函数装饰器和方法装饰器得区别 ,方法装饰器得第一个参数是self本身,所以函数装饰器不能用

  5. python框架之django

    python框架之django 本节内容 web框架 mvc和mtv模式 django流程和命令 django URL django views django temple django models ...

  6. 第六篇:web之python框架之django

    python框架之django   python框架之django 本节内容 web框架 mvc和mtv模式 django流程和命令 django URL django views django te ...

  7. Python框架之Django的相册组件

    Python框架之Django的相册组件 恩,没错,又是Django,虽然学习笔记已经结贴,但是学习笔记里都是基础的,Django的东西不管怎么说还是很多的,要学习的东西自然不会仅仅用十几篇博文就能学 ...

  8. Python框架之Django学习

    当前标签: Django   Python框架之Django学习笔记(十四) 尛鱼 2014-10-12 13:55 阅读:173 评论:0     Python框架之Django学习笔记(十三) 尛 ...

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

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

随机推荐

  1. ph 提交代码的步骤;

    ph 提交代码的步骤: git status 查看状态: ls -ah 查看文件: git stash list 查看本地缓存的文件: git branch 查看本地的分支: git checkout ...

  2. ffmpeg中AVOption的实现分析

    [时间:2017-10] [状态:Open] [关键词:ffmpeg,avutil,AVOption] 0 引言 AVOptions提供了一种通用的options机制,可以用于任意特定结构的对象. 本 ...

  3. 织梦dedecms安全设置详情

    第一.安装的时候数据库的表前缀,最好改一下,不用dedecms默认的前缀dede_,可以改成ljs_,随便一个名称即可. 第二.后台登录开启验证码功能,将默认管理员admin删除,改成一个自己专用的, ...

  4. SparkThriftServer 源码分析

    目录 版本 起点 客户端--Beeline 服务端 Hive-jdbc TCLIService.Iface客户端请求 流程 SparkThrift 主函数HiveThriftServer2 Thrif ...

  5. React+Webpack+Webstorm开发环境搭建

    需要安装的软件 node.js npm包管理 Webstorm 由于6.3.0版本之后会自带npm的包管理所以不需要单独的安装npm nodejs(包含npm)安装在默认路径C:\Program Fi ...

  6. ubuntu cli

    查看安装包的路径 dpkg -L redis-server 防火墙 ufw redis 相关 service redis-server restart

  7. UEditor在asp.netMVC4中的使用,包括上传功能,粘贴表格不显示边框问题

    网页编程中在线编辑器的使用还是很重要的,最近研究了一下百度出的UEditor编辑器,把它结合到刚学的asp.netMVC+EF中,同时实现上传资料(包括图片,视频等)功能,下面就以一个最简单的新闻管理 ...

  8. nodejs在windows下的安装配置(使用NVM的方式)

    NVM的安装 1.下载安装包,https://github.com/coreybutler/nvm-windows/releases 2.下载完成后点击nvm-setup,按步骤安装,注意路径中不能带 ...

  9. SpringBoot------自定义Logback日志

    帮助文档: https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-featur ...

  10. windows 驱动开发 MDL 内核层 用户层共享内存

    参考资料 https://blog.csdn.net/wdykanq/article/details/7752909 http://blog.51cto.com/laokaddk/404584 内核层 ...