views:

99

answers:

1

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.

A: 

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?

Daniel Roseman
Yes, also on the W3C's validator (this is where I initially noticed it).
Zack
Oh wow, I'm quite a moron. I just realized that it was a child template overwriting the code in the base template. So this would be correct as Django's template engine is indeed not capable of making this change.
Zack