嘿,我正在按照这个教程学习如何用Django创建一个wiki页面。但是,它是用django 0.96制作的,我使用django 1.3,所以有些东西是不同的。有些我自己已经修好了,但是这个我好像做不到。
我做了一个表单,将数据提交给视图。 这是表格:
<form method="post" action"/wikicamp/{{page_name}}/save/">{% csrf_token %}
<textarea name="content" rows="20" cols="60">{{content}}</textarea><br>
<input type="submit" value="Save Page"/>
</form>
并且/wikicamp/{{page{u name}/save/url重定向到save\u页面视图:
from django.http import HttpResponseRedirect
from django.core.context_processors import csrf
def save_page(request, page_name):
c = {}
c.update(csrf(request))
content = c.POST["content"]
try:
page = Page.objects.get(pk=page_name)
page.content = content
except Page.DoesNotExist:
page = Page(name=page_name, content=content)
page.save()
return HttpResponseRedirect("wikicamp/" + page_name + "/")
但是问题是我得到了这个错误:
Help
Reason given for failure:
CSRF token missing or incorrect.
In general, this can occur when there is a genuine Cross Site Request Forgery, or when Django's CSRF mechanism has not been used correctly. For POST forms, you need to ensure:
The view function uses RequestContext for the template, instead of Context.
In the template, there is a {% csrf_token %} template tag inside each POST form that targets an internal URL.
If you are not using CsrfViewMiddleware, then you must use csrf_protect on any views that use the csrf_token template tag, as well as those that accept the POST data.
You're seeing the help section of this page because you have DEBUG = True in your Django settings file. Change that to False, and only the initial error message will be displayed.
You can customize this page using the CSRF_FAILURE_VIEW setting.
所以我阅读了一些文档,比如http://docs.djangoproject.com/en/dev/ref/contrib/csrf/#how-to-use-it。我试着去做,但还是犯了同样的错误。
那么:有人知道如何用Django 1.3处理表单post数据吗?
我认为这与以下内容有关:view函数使用RequestContext作为模板,而不是Context。但我现在不知道是什么了。
顺便说一下,在显示本地主机的http请求的终端中,它说:模板中使用了{%csrf_token%},但上下文没有提供该值。这通常是由于不使用RequestContext造成的。
您必须在表单模板的
<form>
标记之间包含{% csrf_token %}
。如果
csrf_token
没有呈现到您的表单中,请确保您在视图的响应中提供了RequestContext
:或者,使用此快捷方式:
使用泛型视图时,
RequestContext
始终可用。您需要{%csrf_token%}模板标记在标记之间,并包括
在applications settings.py中的中间件类中
添加一些示例post数据处理:
这是我在视图中使用POST数据的一个例子。我通常会依赖form类通过清理的数据数组进行验证。
我想你错过了申报表中的符号“=”。
幸运的是,这可能不是一个错误。 因此,如果这不是一个解决方案,请尝试一些更简单的示例:
希望这能奏效
相关问题 更多 >
编程相关推荐