In my template I need to print some items and add "," after each one, unless it is the last item.
So here is my code:
<li>
<strong>Category</strong>:
{% spaceless %}
{% for t in project.technology.all %}
{{ t.title }}
{% if not forloop.last %},{% endif %}
{% endfor %}
{% endspaceless %}
</li>
While it works, it adds an extra whitespace between each item and ",", so what I see is like this:
Tech1 , Tech2 , Tech3
while it should be Tech1, Tech2, Tech3
Even spaceless
template tag is not working.
As the documentation of {% spaceless %}
[Django-doc] says:
Only space between tags is removed – not space between tags and text.
It is thus only the space between tags, not between tags and text, text and text, etc.
You can render this with:
<li>
<strong>Category</strong>:
{% for t in project.technology.all %}
{{ t.title }}{% if not forloop.last %}, {% endif %}
{% endfor %}
</li>
we thus will not render a space between {{ t.title }}
and the comma, since these are all template tags that do not output anything.