我正在尝试在Django模板中创建由逗号分隔但在最后一个项目之后没有逗号的项目列表。项目列表通过for循环内的for循环来。我试图做这样的事情。
{% for thing in things %}
{% spaceless %}
{% for o in thing.otherthing_set.all %}
<span>{{ o.name }}: </span>
{% if o.attribute == 'this' %}
<span>{{ o.otherattribute}}</span>
{% if not forloop.last %}
<span>, </span>
{% endif %}
{% endif %}
{% endfor %}
{% endspaceless %}
<br>
{% endfor %}
但是有时会有一个额外的逗号,因为有时对于循环通过o.attribute =='this'的最后一项并不总是如此。
我想做的是
{% for o in thing.otherthing_set.all %}
甚至在通过for循环之前,仅将其过滤为o.attribute =='this',但似乎不可能做到这一点。
有什么建议吗?