<p>应用<a href="https://docs.djangoproject.com/en/dev/ref/templates/builtins/#safe" rel="nofollow">^{<cd1>}</a>过滤器将把<em>任何</em>变成一个字符串。如果你从字面的<code>[1, 2, 'foo', u'bar']</code>开始,你最终会得到近似字面的<code>u"[1, 2, 'foo', u'bar']"</code>(或者类似的东西——我不太确定它是如何呈现的,因为我从未尝试过这样做;我也说“近似”,因为它实际上是一个<code>SafeString</code>实例,而不是一个<code>unicode</code>实例)。然后,迭代将遍历生成的字符串中的每个字符,这不是您想要的。</p>
<p>相反,您可以使用<a href="https://docs.djangoproject.com/en/dev/ref/templates/builtins/#safeseq" rel="nofollow">^{<cd6>}</a>过滤器,它将<code>safe</code>过滤器应用于序列中的每个元素</p>
<pre><code><ul>
{% for author in authors|safeseq %}
<li>{{ author }}</li>
{% endfor %}
</ul>
</code></pre>
<p>或者,可以对迭代器中的值应用<code>safe</code>。</p>
<pre><code><ul>
{% for author in authors %}
<li>{{ author|safe }}</li>
{% endfor %}
</ul>
</code></pre>
<p>我建议您使用<code>safeseq</code>,因为如果您只想显示值,那么您可以使用<a href="https://docs.djangoproject.com/en/dev/ref/templates/builtins/#unordered_list" rel="nofollow">^{<cd10>}</a>过滤器进一步优化模板。(请注意,我不确定它的行为方式,这可能会使它失去安全性。你得试试。)</p>
<pre><code><ul>{{ authors|safeseq|unordered_list }}</ul>
</code></pre>