Django CSRF 验证
我无法解决 CSRF 令牌验证 403 错误。 这是我所拥有的:
视图:
from django.template import RequestContext
def add(request, name):
#logic
return render_to_response("category/add.html", {"form": form},
context_instance = RequestContext(request))
模板:
<form action="/category/add/" method="post">
{% csrf_token %}
New Category: <input type="text" name="name" id="categoryname" />
<input type="submit" value="Hinzufuegen" />
</form>
设置:
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',
)
I can't get around the CSRF token validation 403 error.
Here's what I have:
view:
from django.template import RequestContext
def add(request, name):
#logic
return render_to_response("category/add.html", {"form": form},
context_instance = RequestContext(request))
template:
<form action="/category/add/" method="post">
{% csrf_token %}
New Category: <input type="text" name="name" id="categoryname" />
<input type="submit" value="Hinzufuegen" />
</form>
settings:
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',
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否将 POST 数据传递到表单进行验证?你必须做这样的事情:
Are you passing the POST data to the form for validation? You have to do something like this:
似乎您需要添加
到您的 MIDDLEWARE_CLASSES 中。似乎它没有验证 CSRF 令牌,只是生成......
帮助我我的案例 ...
Seems like you need to add
to your MIDDLEWARE_CLASSES. Seems that it is not validating CSRF token, just generating...
helped me in my case...