Django的模板变量被解释为HTML

时间:2013-08-11 18:34:17

标签: html django templates django-templates

DEBUG=True包含HTML的变量刚刚打印在模板中且作为HTML插入时。这是正确的行为。

另一方面,当DEBUG=False(只是改变这个)时,变量的内容被解释为HTML,我无法理解为什么。我没有使用safe过滤器。

我的模板如下:

  <pre id="copy-source-{{ forloop.counter }}">
    <code>{{ code }}</code>
  </pre>

在一个案例中,code恰好是HTML代码,具体为:

<a href="blabla"><img src="bblabla" /></a>

并且页面显示图像而不是代码,即使它在预编码内!?

1 个答案:

答案 0 :(得分:2)

试试这个

{% autoescape off %}
    {{var_containing_html}}
{% endautoescape %}

我希望这会有所帮助。 祝你好运。

P.S。另请参阅docs

相关问题