CSRF 令牌丢失或不正确(我知道,之前已被问过!)
这两天在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
render_to_response 的签名是 render_to_response(template_name[,dictionary][, context_instance][, mimetype]) 所以您应该像下面这样调用它以确保 csrf 令牌放入上下文中吗?
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?你应该查看这个文档
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