Django Rest Framework取消csrf
2019-02-15 本文已影响10人
一只肥豚鼠
首先写一个中间件
from rest_framework.authentication import SessionAuthentication, BasicAuthentication
class CsrfExemptSessionAuthentication(SessionAuthentication):
def enforce_csrf(self, request):
return # To not perform the csrf check previously happening
然后在setting中设置这个中间件
在每个类中添加一段代码
authentication_classes = (CsrfExemptSessionAuthentication, BasicAuthentication)