作者sean72 (.)
看板Python
标题[问题] Django Rest Framework authentication
时间Fri Sep 2 06:36:17 2016
我想关闭某个post的 CSRF
http://www.django-rest-framework.org/api-guide/viewsets/#viewset
里面提到
You can use any of the standard attributes such as permission_classes,
authentication_classes in order to control the API policy on the viewset.
stack overflow查到
http://goo.gl/k082op
所以我在我的view.py里面加入
from rest_framework.authentication import
SessionAuthentication, BasicAuthentication,
class CsrfExemptSessionAuthentication(SessionAuthentication):
def enforce_csrf(self, request):
print('csrf exempt...')
#从没跑到这行
return
class ItemViewSet(viewsets.ModelViewSet):
queryset = Item.objects.all()
serializer_class = ItemSerializer
# 并且设定authentication_classes
authentication_classes = (CsrfExemptSessionAuthentication,
BasicAuthentication)
但是我仍然得到
Forbidden (CSRF cookie not set.)
请问我哪边做错了?
谢谢
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 204.96.168.3
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1472769386.A.336.html
1F:→ frank910138: 在function上面加上 @csrf_exempt 试试看 ,是不是 09/02 11:38
2F:→ frank910138: 你要的 09/02 11:38
3F:→ sean72: doesn't work 09/04 07:13
4F:→ kevinkung: 有import csrf_exempt吗? 10/06 23:57