【发布时间】:2018-08-21 14:45:30
【问题描述】:
我正在尝试在 Django 中在基于类的视图上设置 csrf_exempt 我已经尝试过这个:
现在,我的班级是这样的:
class UserView(View):
# Check csrf here
def get(self, request, pk):
#code here
# I want to exempt csrf checking only on this method
def post(self, request):
#code here
同时,如果我使用@method_decorator(csrf_exempt, name='dispatch'),它将应用于类中的每个方法。在 Django 的基于类的视图中,仅对特定方法进行豁免的最佳方法是什么?
【问题讨论】:
-
CSRF 仅在 POST 的上下文中才有意义。它不是在 GET 上强制执行的。
-
@DanielRoseman crsf 也适用于 PUT、DELETE 等
-
@HenryM 我只想将
csrf_exempt应用于特定方法。例如只发给put或只发给post