django中CBV加csrf_exempt函数问题

CSRF Token相关装饰器在CBV只能加到dispatch方法上

备注:

1. csrf_protect,为当前函数强制设置防跨站请求伪造功能,即便settings中没有设置全局中间件。
2. csrf_exempt,取消当前函数防跨站请求伪造功能,即便settings中设置了全局中间件
from django.views.decorators.csrf import csrf_exempt, csrf_protect
 
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/")
原文地址:https://www.cnblogs.com/taosiyu/p/11761335.html