How do I check for last loop iteration in Django template?

前端 未结 2 1841
忘了有多久
忘了有多久 2020-12-24 11:25

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?

相关标签:
2条回答
  • 2020-12-24 11:55

    {{ forloop.last }}

    0 讨论(0)
  • 2020-12-24 11:56

    You would use forloop.last. For example:

    <ul>
    {% for item in menu_items %}
        <li{% if forloop.last %} class='last'{% endif %}>{{ item }}</li>
    {% endfor %}
    </ul>
    
    0 讨论(0)
提交回复
热议问题