How do I check for last loop iteration in Django template?
-
10-07-2019 - |
Question
I have a basic question, in the Django template language how can you tell if you are at the last loop iteration in a for
loop?
Solution
You would use forloop.last
. For example:
<ul>
{% for item in menu_items %}
<li{% if forloop.last %} class='last'{% endif %}>{{ item }}</li>
{% endfor %}
</ul>
OTHER TIPS
{{ forloop.last }}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow