开发者

Django template lists

I am relatively new to Django and I have a simple problem. In my template I have:

{% for article in article_list %}
<li>
{% for author in article.authors.all %}
        {{ author.name }}, 
{% endfor %}
{{ article.title }}
</li>
{% endfor %}

What I want is to be able to iterate over the author.name but the last name should end with a period not a comma. Do I have to go back to the view to achieve this, or is there s开发者_如何学编程ome simple way in the template?


In templates, for loops have a first and last attribute, so you can do this:

{% for author in article.authors.all %}
  {{ author.name }}{% if not forloop.last %},{% else %}.{% endif %}
{% endfor %}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜