我在写一个Django应用程序。这里是我的代码:表单提交中的Django CSRF错误
edit.module.html(模板;除去多余的HTML标记):
<h1>Enter the HTML below</h1>
<form action="./update/" method="post">
{% csrf_token %}
<textarea cols='55' rows='15'></textarea>
<input type='submit' value='Submit' />
</form>
urls.py
url(r'^myapp/update/$', 'myproj.myapp.views.update_module'),
view.py
from django.http import HttpResponse
from django.shortcuts import render_to_response, get_object_or_404
from django.core.context_processors import csrf
from django.template import RequestContext
#Select the module you want to edit
def edit_modules(request):
lpconf = {"module_to_edit" : "top"}
return render_to_response('admin/edit.module.html', lpconf, context_instance=RequestContext(request))
def update_module(request):
return render_to_response('admin/updated.html', context_instance=RequestContext(request))
当我提交表单时,我得到CSRF错误:“CSRF验证失败。请求中止“
我跟着Django文档(https://docs.djangoproject.com/en/dev/ref/contrib/csrf/),并试图解决这个问题,但我不能我在做什么错在这里
感谢
UPDATE:。?。更新视图。 。PY功能edit_modules和update_modules edit_modules是一个呈现形式和update_modules是一个处理它吧,我没有得到CSRF错误我现在得到的错误:空模块名
更新:我是能够修复它,我正在使用一个vie w呈现表单,另一个表单处理表单。我不得不将上下文添加到呈现表单的第一个视图中。
我是否需要传递一个实际的模板html文件才能使其工作?我不能仅仅传递一些文本消息而不必使用实际的HTML文件? – Blueboye
你可能应该定义一个合适的HTML模板,它不需要花一点时间在模板目录中创建一个模板! –
是的,我刚刚做了,并在视图中我更新了代码“return render(request,'updated.html')”。 Sill相同的CSRF错误。我还导入了“from django.shortcuts import render” – Blueboye