bartekch
bartekch

Reputation: 623

Django. How to display list of objects in one line using for loop in templates?

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.

Upvotes: 2

Views: 4325

Answers (2)

Mihai Zamfir
Mihai Zamfir

Reputation: 2166

Simpler solution than the one given by Totem:

{{ my_objects|join:', ' }}

Upvotes: 6

0xmax
0xmax

Reputation: 543

Did you try ?

<p>
{% for object in my_objects %} 
    {{ object }}{% if not forloop.last %},{% endif %} 
{% endfor %}
</p>

Upvotes: 6

Related Questions