Django管理员引发CSRF验证失败 [英] Django admin raises CSRF verification failed

查看:1003
本文介绍了Django管理员引发CSRF验证失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始新的django项目并启用了管理员应用程序。
我可以登录管理员网站,但是当我试图添加/更改网站或用户我得到

  CSRF验证失败。请求中止。 
失败的原因:
CSRF令牌丢失或不正确。

这是我在settings.py中的:

  TEMPLATE_LOADERS =(
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',


MIDDLEWARE_CLASSES =(
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django。中间件csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
#取消注释下一行为简单点击保护:
#'django.middleware.clickjacking.XFrameOptionsMiddleware',

INSTALLED_APPS =(
'django.contrib.auth',
'django.contrib .contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles ',
'django.contrib.admin',
#取消注释下一行以启用管理员文档:
#'django.contrib.admindocs',

当我查看管理页面源时,我看到

 < input type ='hidden' name ='csrfmiddlewaretoken'value ='T9Mfk1LRXi5jPE2dh5jcvdKwzYM6Iy5I'/> 



我有Django版本1.4 .1

解决方案

您是否覆盖了CSRF_COOKIE_DOMAIN设置?如果CSRF令牌存在于表单中,并且您尚未修改管理应用程序的源代码,那么最可能的情况是cookie未正确设置。



检查登录页面的响应标题,以确保cookie设置正确,并检查登录尝试的请求标头,以确保它也被发送(并与表单中的值相匹配)。 / p>

I've started new django project and enabled admin app. I can login to admin site but when I'm trying to add/change site or user I'm getting

CSRF verification failed. Request aborted.
Reason given for failure:
CSRF token missing or incorrect.

That's what I have in settings.py:

TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)

MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)

When I'm looking at admin page source I see

<input type='hidden' name='csrfmiddlewaretoken' value='T9Mfk1LRXi5jPE2dh5jcvdKwzYM6Iy5I' />

there

I have Django version 1.4.1

解决方案

Have you overridden the CSRF_COOKIE_DOMAIN setting? If the CSRF token is present in the form, and you haven't modified the source of the admin app, then the most likely scenario is that the cookie is not being set correctly.

Check the response headers of the login page to make sure that the cookie is being set correctly, and check the request headers of your login attempt to ensure that it is also being sent (and matches the value in the form).

这篇关于Django管理员引发CSRF验证失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆