Django Template For Loop Removing <img> Self-Closing

Posted by Zack on Stack Overflow See other posts from Stack Overflow or by Zack
Published on 2010-02-23T00:52:28Z Indexed on 2010/03/31 21:03 UTC
Read the original article Hit count: 264

Django's for loop seems to be removing all of my <img> tag's self-closing...ness (/>). In the Template, I have this code:

{% for item in item_list %}
<li>
    <a class="left" href="{{ item.url }}">{{ item.name }}</a>
    <a class="right" href="{{ item.url }}">
        <img src="{{ item.icon.url }}" alt="{{ item.name }} Logo." />
    </a>
</li>
{% endfor %}

It outputs this:

<li>
    <a class="left" href="/some-url/">This is an item</a>
    <a class="right" href="/some-url/">
        <img src="/media/img/some-item.jpg" alt="This is an item Logo.">
    </a>
</li>

As you can see, the <img> tag is no longer closed, and thus the page doesn't validate. This isn't a huge issue since it'll still render properly in all browsers, but I'd like to know how to solve it. I've tried wrapping the whole for loop in {% autoescape off %}...{% endautoescape %} but that didn't change anything. All other self-closed <img> tags in the document outside the for loop still properly close.

© Stack Overflow or respective owner

Related posts about django

Related posts about django-templates