Django Template and XML Question

I have this Django view that does render_to_response (rss.xml, {"list": list}) with this list:

<a href="link.html">description</a>
<a href="link2.html">description2</a>
<a href="link3.html">description3</a>

The rss.xml template is as follows:

<?xml version="1.0" encoding="UTF-8"?><rss version="0.92">
        {% for item in list%}
        {{item}}
        {% endfor %}

This works, however, <and "replace them with special html charactervalues, such as:

<?xml version="1.0" encoding="UTF-8"?><rss version="0.92">
&lt;a href=&quot;link.html&quot;&gt;Description&lt;/a&gt;
&lt;a href=&quot;link2.html&quot;&gt;Description2&lt;/a&gt;
&lt;a href=&quot;link3.html&quot;&gt;Description3&lt;/a&gt;

how can I just output the raw lines so that the document becomes:

<?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>
+3
source share
2 answers

Replace {{item}}with {{item|safe}}in your code. This avoids HTML screens. See this document page for more information .

+3
source

You should surround the for block with autoescape tags as follows:

<?xml version="1.0" encoding="UTF-8"?><rss version="0.92">
{% autoescape off %}
    {% for item in list%}
    {{item}}
    {% endfor %}
{% endautoescape %}

django will not exit characters between autoescape tags

see here: https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#autoescape

+5
source

All Articles