• 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/")
  • 相关阅读:
    win中使用curl上传文件报错
    S2-052
    S2-048
    S2-045、S2-046
    S2-033、S2-037
    S2-032
    S2-029
    day12-python之深灰魔法
    day10-11-python基础之字符串
    day09-python基础
  • 原文地址:https://www.cnblogs.com/wtil/p/11623237.html
Copyright © 2020-2023  润新知