drf中报错 "detail": "CSRF Failed: CSRF cookie not set."

解决方案

  • 引入django的中间件 MiddlewareMixin
from django.utils.deprecation import MiddlewareMixin
  • 完成中间件的书写
class NotUseCsrfTokenMiddlewareMixin(MiddlewareMixin):
    def process_request(self,request):
        setattr(request, '_dont_enforce_csrf_checks', True)
  • 注册中间件
MIDDLEWARE = [
    'xxx.NotUseCsrfTokenMiddlewareMixin',
]

你可能感兴趣的:(drf中报错 "detail": "CSRF Failed: CSRF cookie not set.")