Zack
Zack

Reputation: 2284

Django Template For Loop Removing <img> Self-Closing

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.

Upvotes: 1

Views: 422

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599826

Django's template engine is not capable of making this change. Are you sure this is the code you see when you do View Source?

Upvotes: 1

Related Questions