当前位置: 首页 > 面试题库 >

CSRF验证失败。请求中止。在Django上

毕泽宇
2023-03-14
问题内容

我正在关注Django 1.3 Web开发。对于登录,我收到以下错误

Forbidden (403)
CSRF verification failed. Request aborted.
Help
Reason given for failure:
    CSRF token missing or incorrect.

这是我的settings.py随附的APPS。这正是书上所说的样子。

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
    'djangocricket.Cricket',
    'djangocricket.cms'
)

这本书说,它应该包含django.contrib.auth.views.login ..并且我将其包含在

urlpatterns = patterns('',
    # Examples:
    url(r'^$', 'djangocricket.Cricket.views.index', name='default'),
    url(r'^user/(\w+)/$', 'djangocricket.Cricket.views.user_home', name='user home'),
    url(r'^login/$', 'django.contrib.auth.views.login'),
    # url(r'^djangocricket/', include('djangocricket.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    #url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^news/', 'djangocricket.cms.views.index', name='index'),
    #url(r'^news/(?P<slug>[^\.]+).html', 'djangocricket.cms.views.detail', name='get_single_news_item'),
    url(r'^admin/', include(admin.site.urls)),
)

和我的registration / login.html …复制自本书的副本。它应该做。

<html>
<head>
    <title>Django Bookmarks - User Login</title>
</head>
<h1>User Login</h1>
{% if form.errors %}
    <p>Your username and password didn't match.
        Please try again.</p>
{% endif %}
<form method="post" action=".">
    <p><label for="id_username">Username:</label>
        {{ form.username }}</p>
    <p><label for="id_password">Password:</label>
        {{ form.password }}</p>
    <input type="hidden" name="next" value="/" />
    <input type="submit" value="login" />
</form>
</body>
</html>

问题答案:

你需要将{% csrf_token %}模板标记添加为formDjango模板中元素的子代。

这样,模板将呈现一个隐藏元素,其值设置为CSRF令牌。当Django服务器收到表单请求时,Django将验证令牌与表单中呈现的值匹配。这对于确保POST请求(即,数据更改请求)源自真实的客户端会话是必要的。



 类似资料:
  • 问题内容: 我尝试建立一个非常简单的网站,在该网站中可以将数据添加到sqlite3数据库中。我有两个文本输入的POST表单。 index.html: forms.py: views.py: 但是,当我单击“提交”按钮时,出现403错误: 我已经包含{% csrf_token %}在index.html中。但是,如果这是一个RequestContext问题,我真的不知道在哪里以及如何使用它。我希望所

  • 问题内容: 尝试从教程中制作简单表格时,我收到CSRF验证失败的消息。我对CSRF验证实际上是什么进行了一些研究,据我所知,要使用CSRF验证,你需要在html中使用其中的csrf_token标记之一,但是我没有 这是我的模板: 相当简单,位于contact.html 这是我的urlconf:从 应用程序名称为testapp1。当我键入URL(http:// localhost:8000 / te

  • 问题内容: 禁止(403)CSRF验证失败。请求中止。即使使用{%csrf_token%}我正在尝试在django中进行登录,但出现此错误,我检查了CSRF文档,但对我来说没有任何用处。 这是HTML: 就像你在上面看到的那样,我使用了{%csrf_token%},并且在已安装的应用程序中有“ django.middleware.csrf.CsrfViewMiddleware”。 我的看法是: 我

  • 但是这个配置的问题是我只能以用户Euclid的身份登录。但我想作为每一个可用的用户登录。 但是对于另一个配置,我会在日志中得到这样的消息,即找到了该用户,但已经发生了驱逐。 2017-04-20 09:36:16]ldap_driver.debug:ldap_search(dc=example,dc=com,(&(&(objectclass=person))(UID=einstein)),[arr

  • 问题内容: 我可以通过我的AJAX帖子向遵循Django CSRF保护机制的人员提供帮助。我按照这里的指示进行: http://docs.djangoproject.com/en/dev/ref/contrib/csrf/ 我已经精确地复制了他们在该页面上拥有的AJAX示例代码: http://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax

  • 问题内容: 我可以通过我的AJAX帖子向遵循Django CSRF保护机制的人员提供帮助。我按照这里的指示进行: http://docs.djangoproject.com/en/dev/ref/contrib/csrf/ 我已经精确地复制了他们在该页面上拥有的AJAX示例代码: http://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax