django中CBV加csrf_exempt函数问题

2023-02-14,,

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/")

django中CBV加csrf_exempt函数问题的相关教程结束。

《django中CBV加csrf_exempt函数问题.doc》

下载本文的Word格式文档,以方便收藏与打印。