3

我对 django csrf 中间件有问题...当我使用模板标签 csrf_token 时,我得到以下输出:

<div style='display:none'><input type='hidden' name='csrfmiddlewaretoken' value='6bda3605af31dd8595d2a67d0dda827b' /></div>

但我想要这个输出(HTML不是xHTML:

<div style='display:none'><input type='hidden' name='csrfmiddlewaretoken' value='6bda3605af31dd8595d2a67d0dda827b'></div>

我试图在 django.middleware.csrf.CsrfViewMiddleware 中查看代码但没有成功:(

那么,如何更改输出 fo csrf_token 标记?

坦克

4

3 回答 3

3

这是我解决这个问题的方法。

{% with csrf_token as csrf_token_clean %}{{ csrf_token_clean }}{% endwith %}

在您的模板中,这将仅返回令牌键,因此您可以编写自己的有效 HTML 输入标记,如下所示。

<input type="hidden" name="csrfmiddlewaretoken" value="{% with csrf_token as csrf_token_clean %}{{ csrf_token_clean }}{% endwith %}" >

来源:http ://www.phptodjango.com/2010/07/django-csrftoken-template-tag-fix.html

于 2010-07-31T18:56:29.860 回答
3

解决方案其实很简单:

<input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">

构造没有意义{% with %}

于 2011-10-22T01:35:41.023 回答
1

你必须去编辑 django.template.defaulttags.py:在第 48 行有标签的输出,你可以随意改变它。

请注意,这是一项开发功能,因此可能会发生变化 - 更新 Django 可能会删除您的更改!。
另外,请花点时间寻找有关此特定问题的票:我提出的解决方案可能会解决您的问题,但我认为“官方”解决方案会更好。

于 2010-01-31T16:03:35.337 回答