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',
]
posted @ 2020-06-04 22:05  巫小诗  阅读(1141)  评论(0编辑  收藏  举报