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/")
  • 相关阅读:
    开发流程之功能设计
    spring security之web应用安全
    feign架构原理解析
    负载均衡之ribbon
    服务发现之eureka
    JavaScript的内置对象
    JavaScript的流程控制语句以及函数
    JavaScript的基础语法及DOM元素和事件
    CSS的基础使用
    盒子模型
  • 原文地址:https://www.cnblogs.com/wtil/p/11623237.html
Copyright © 2011-2022 走看看