开发者

Django template lists

开发者 https://www.devze.com 2023-03-28 12:48 出处:网络
I am relatively new to Django and I have a simple problem. In my template I have: {% for article in article_list %}

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

精彩评论

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