"detail": "CSRF Failed: CSRF cookie not set."
二.解决办法方法一:
在配置文件中配置
REST_FRAMEWORK = { 'DEFAulT_AUTHENTICATION_CLASSES': ( 'rest_framework.authentication.TokenAuthentication',)}
方法二.在提交信息中加上csrf_token:
页面form框中设置
{% csrf_token %}
这代码在页面中的显示内容<input type="hIDden" name="csrfmIDdlewaretoken" value="l9gICFdVzkDkLExUoAPRpE0ElkGtG70Xmn6u536Keo3kaQPXEeC00g5kbnY3vJGd">
如果是AJAX提交的话
在data中加上
data:{ .. .. 'csrfmIDdlewaretoken': '{{csrf_token}}' //或者 'csrfmIDdlewaretoken':'l9gICFdVzkDkLExUoAPRpE0ElkGtG70Xmn6u536Keo3kaQPXEeC00g5kbnY3vJGd'}
三.奇怪现象肯能与django中中间件有冲突django中间件MIDDLEWARE = [ 'django.mIDdleware.security.SecurityMIDdleware','django.contrib.sessions.mIDdleware.SessionMIDdleware','django.mIDdleware.common.CommonMIDdleware','django.mIDdleware.csrf.CsrfVIEwMIDdleware','django.contrib.auth.mIDdleware.AuthenticationMIDdleware','django.contrib.messages.mIDdleware.MessageMIDdleware','django.mIDdleware.clickjacking.XFrameOptionsMIDdleware',]
如果在中间件中把'django.mIDdleware.csrf.CsrfVIEwMIDdleware',
注释掉你用方法二的时候也会报错,只有方法一能正常使用
以上是内存溢出为你收集整理的DRF框架中csrf异常全部内容,希望文章能够帮你解决DRF框架中csrf异常所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)