Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django template and XML question

Tags:

python

xml

django

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 the following:

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

This works, however the <'s and "'s get replaced by their special html charactervalues like:

<?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 strings such 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>
like image 987
Javaaaa Avatar asked Jan 27 '26 12:01

Javaaaa


1 Answers

You should surround the for block with autoescape tags like so:

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

django won't escape the characters between the autoescape tags

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

like image 151
Blake Avatar answered Jan 30 '26 00:01

Blake