我有这个Django视图,它使用此列表执行render_to_response(rss.xml,{“list”:list}):
<a href="link.html">description</a>
<a href="link2.html">description2</a>
<a href="link3.html">description3</a>
rss.xml模板如下:
<?xml version="1.0" encoding="UTF-8"?><rss version="0.92">
{% for item in list%}
{{item}}
{% endfor %}
这是有效的,但&lt;和s'取代了他们特殊的html字符值,如:
<?xml version="1.0" encoding="UTF-8"?><rss version="0.92">
<a href="link.html">Description</a>
<a href="link2.html">Description2</a>
<a href="link3.html">Description3</a>
如何输出原始字符串,使文档变为:
<?xml version="1.0" encoding="UTF-8"?><rss version="0.92">
<a href="link.html">description</a>
<a href="link2.html">description2</a>
<a href="link3.html">description3</a>
答案 0 :(得分:5)
你应该使用autoescape标签包围for块,如下所示:
<?xml version="1.0" encoding="UTF-8"?><rss version="0.92">
{% autoescape off %}
{% for item in list%}
{{item}}
{% endfor %}
{% endautoescape %}
django不会转义autoescape标签之间的字符
见这里: https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#autoescape
答案 1 :(得分:3)
在代码中将{{item}}
替换为{{item|safe}}
。它将避免转义HTML字符。有关详细信息,请see this doc page。