[問題] Django Rest Framework CSRF

作者: sean72 (.)   2016-09-02 06:36:17
我想關閉某個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.)
請問我哪邊做錯了?
謝謝
作者: frank910138 (frank)   2016-09-02 11:38:00
在function上面加上 @csrf_exempt 試試看 ,是不是你要的
作者: sean72 (.)   2016-09-04 07:13:00
doesn't work
作者: kevinkung (Jaguar)   2016-10-06 23:57:00
有import csrf_exempt嗎?

Links booklink

Contact Us: admin [ a t ] ucptt.com