[英]Have {% csrf_token %} tag in template but no csrfmiddlewaretoken hidden value
我在 Django 中使用 django.contrib.comments,版本是 1.6.1 然后我有一个这样的表格,
{% get_comment_form for mbean as form %}
<table>
<form action="{% comment_form_target %}" method="post" class="aaa">{% csrf_token %}
{{ form }}
<tr>
<td colspan="2">
<input type="submit" name="submit" value="Post">
<input type="submit" name="preview" value="Preview">
</td>
</tr>
</form>
</table>
但是当我在 HTML 中检查我的页面源时,我在我的源中找不到<input type='hidden' name='csrfmiddlewaretoken' value='uiwp7YkGi374HwnZqMRbCUmzyH38jDPI' />
,并且有
CSRF 验证失败。 请求中止
错误。
我还在这个模板的其他形式中使用了{% csrf_token %}
但仍然没有“csrfmiddlewaretoken”。
没有“csrfmiddlewaretoken”是我出现“CSRF 验证失败。请求中止”错误的原因吗?
任何人都可以就我的模板有什么问题提供帮助吗?
要在模板中使用 {% csrf_token %},您需要在渲染模板时将 RequestContext 实例传递给模板,然后就可以了。 然后您可以使用 csrf_token 并且它也将在源代码中可见。
渲染模板时发送上下文实例的示例代码片段:
from django.template import RequestContext
def home(request):
return render_to_response('index.html', context_instance=RequestContext(request))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.