CSRF 令牌丢失或不正确(我知道,之前已被问过!)

发布于 2025-01-04 05:07:54 字数 2808 浏览 1 评论 0原文

这两天在 Google、stackoverflow 和 docs.djangoproject.com 上寻找 CSRF 问题的解决方案。

免责声明,我是 Django 的初学者,正在阅读这本书Django 的权威指南 - 正确完成 Web 开发。 嗯,显然我遇到了一些问题:-(

请参阅下面的一个这样的尝试,看看你是否可以指出任何错误,因为我已经尝试了 stackoverflow 上的所有建议,但还没有成功:

view.py:

from django.shortcuts import render_to_response
from django.template import RequestContext

def add_vehicle(request):
    return render_to_response('vehicle.html', RequestContext(request, {}))

vehicle.html:

{% extends "base.html" %}
{% block title %}Vehicle Registration{% endblock %}
{% block content %}
<html>
<head>
</head>
<body>

<form action="/vehicle/" method="post"> {% csrf_token %}
    <table width=100%>
        <tr>
            <td>Reg #:</td>
            <td><input type="text" name="regnumber"></td>
            <td></td>
        </tr>
        <tr>
            <td>Model:</td>
            <td><input type="text" name="model"></td>
            <td></td>
        </tr>
        <tr>
            <td>Manufacturer:</td>
            <td><input type="text" name="manufacturer"></td>
            <td></td>
        </tr>
        <tr>
            <td>Year:</td>
            <td><input type="text" name="year"></td>
            <td></td>       
        </tr>
        <tr>
            <td>Chassis #:</td>
            <td><input type="text" name="chasisnumber"></td>
            <td></td>       
        </tr>
        <tr>
            <td></td>
            <td><input type="submit" value="Submit">
            <input type="submit" value="Clear">
            </td>
            <td></td>       
        </tr>
    </table>
</form>
</body>
</html>
{% endblock %}

我希望 stackoverflow 的代码不会太多。

现在,我不断发现 CSRF 令牌丢失或不正确

请协助

编辑(添加错误详细信息)

settings.py 如下所示:

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',
)

这是当我时控制台显示的内容 。打开vehicle.html页面:

warnings.warn("A {% csrf_token %} was used in a template, but the context did not provide the value.  This is usually caused by not using RequestContext.")

Been two days trolling through Google, stackoverflow, and docs.djangoproject.com for a solution to CSRF problems.

Disclaimer, am a beginner at Django and following along the book The definitive guide to django - web development done right. Well, apparently am getting something wrong :-(

See below one such attempt and see if you can point out any mistakes because I've tried all the suggestions on stackoverflow but no luck as yet:

view.py:

from django.shortcuts import render_to_response
from django.template import RequestContext

def add_vehicle(request):
    return render_to_response('vehicle.html', RequestContext(request, {}))

vehicle.html:

{% extends "base.html" %}
{% block title %}Vehicle Registration{% endblock %}
{% block content %}
<html>
<head>
</head>
<body>

<form action="/vehicle/" method="post"> {% csrf_token %}
    <table width=100%>
        <tr>
            <td>Reg #:</td>
            <td><input type="text" name="regnumber"></td>
            <td></td>
        </tr>
        <tr>
            <td>Model:</td>
            <td><input type="text" name="model"></td>
            <td></td>
        </tr>
        <tr>
            <td>Manufacturer:</td>
            <td><input type="text" name="manufacturer"></td>
            <td></td>
        </tr>
        <tr>
            <td>Year:</td>
            <td><input type="text" name="year"></td>
            <td></td>       
        </tr>
        <tr>
            <td>Chassis #:</td>
            <td><input type="text" name="chasisnumber"></td>
            <td></td>       
        </tr>
        <tr>
            <td></td>
            <td><input type="submit" value="Submit">
            <input type="submit" value="Clear">
            </td>
            <td></td>       
        </tr>
    </table>
</form>
</body>
</html>
{% endblock %}

I hope that's not too much code for stackoverflow.

Now, I keep getting the CSRF token missing or incorrect.

Please assist.

Edit (Adding details on error)

settings.py looks like this:

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',
)

Here is what the console displays when I open vehicle.html page:

warnings.warn("A {% csrf_token %} was used in a template, but the context did not provide the value.  This is usually caused by not using RequestContext.")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑色毁心梦 2025-01-11 05:07:54

render_to_response 的签名是 render_to_response(template_name[,dictionary][, context_instance][, mimetype]) 所以您应该像下面这样调用它以确保 csrf 令牌放入上下文中吗?

return render_to_response('vehicle.html', {}, context_instance= RequestContext(request))`

The signature for render_to_response is render_to_response(template_name[, dictionary][, context_instance][, mimetype]) so should you be calling it like the following to ensure the csrf token is put into the context?

return render_to_response('vehicle.html', {}, context_instance= RequestContext(request))`
深居我梦 2025-01-11 05:07:54

你应该查看这个文档
https://docs.djangoproject.com/en/dev/ref/contrib/ csrf/

你可能缺少中间件

You should check out this documentation
https://docs.djangoproject.com/en/dev/ref/contrib/csrf/

You may be missing the middle ware

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文