zoukankan      html  css  js  c++  java
  • Django-csrf装饰器FBV和CBV的区别

    # csrf_protect,为当前函数强制设置防跨站请求伪造功能,即便settings中没有设置全局中间件。
    # csrf_exempt,取消当前函数防跨站请求伪造功能,即便settings中设置了全局中间件
    
    from django.utils.decorators import method_decorator
    from django.views.decorators.csrf import csrf_exempt,csrf_protect
    
    # 在FBV中可以加在方法上
    @method_decorator(csrf_exempt)
    def  reg(request):
        .....
    
    # 在CBV中必须加在dispatch上
    # 方法一
    class HomeView(View):
    
        @method_decorator(csrf_exempt)
        def dispatch(self, request, *args, **kwargs):
            # 在请求到来之前,都会执行dispatch函数
            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/")
    
    # 方法二
    @method_decorator(csrf_exempt,name='dispatch')
    class HomeView(View):
    
        def get(self, request):
            return render(request, "home.html")
    
        def post(self, request):
            print("Home View POST method...")
            return redirect("/index/")
  • 相关阅读:
    文件重名问题
    文件上传
    回顾IO流
    Freemarker
    中文乱码问题
    Filter(过滤器)
    Ajax
    jQuery
    普华操作系统,开机无法进入桌面程序; 解决多次source /etc/profile的烦恼
    C++ 文件类型判别错误,将目录文件识别为普通文件
  • 原文地址:https://www.cnblogs.com/wtil/p/11623237.html
Copyright © 2011-2022 走看看