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

跟風遠走 提交于 2019-12-24 12:39:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!