277👍
✅
You would use forloop.last
. For example:
<ul>
{% for item in menu_items %}
<li{% if forloop.last %} class='last'{% endif %}>{{ item }}</li>
{% endfor %}
</ul>
- [Django]-How to obtain and/or save the queryset criteria to the DB?
- [Django]-Django REST Framework – 405 METHOD NOT ALLOWED using SimpleRouter
- [Django]-How to concatenate strings in django templates?
1👍
You can basically use this logic in a for
loop:
{% if forloop.last %}
# Do something here
{% endif %}
For example, if you need to put a comma after each item except for the last one, you can use this snippet:
{% for item in item_list %}
{% if forloop.last %}
{{ item }}
{% else %}
{{ item }},
{% endif %}
{% endfor %}
which will become for a list with three items:
first_item, second_item, third_item
- [Django]-Switching to PostgreSQL fails loading datadump
- [Django]-Update only specific fields in a models.Model
- [Django]-Suppress "?next=blah" behavior in django's login_required decorator
Source:stackexchange.com