问题
I want to display list of objects in one line using django template tags, That piece of code:
{% for object in my_objects %}
<p> {{ object }} </p>
{% endfor %}
gives me that kind of result:
obj1
obj2
obj3
.
.
.
etc.
Is there any way, to get that kind of result(with comas):
obj1, obj2, obj3, ... etc.
Thank You for anwsers.
回答1:
Did you try ?
<p>
{% for object in my_objects %}
{{ object }}{% if not forloop.last %},{% endif %}
{% endfor %}
</p>
回答2:
Simpler solution than the one given by Totem:
{{ my_objects|join:', ' }}
来源:https://stackoverflow.com/questions/27356191/django-how-to-display-list-of-objects-in-one-line-using-for-loop-in-templates