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 %}
精彩评论