Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 547
Daniel Kivatinos Avatar asked May 07 '09 21:05

Daniel Kivatinos


People also ask

What is the use of Forloop counter in Django?

forloop. counter: By using this, the iteration of the loop starts from index 1. forloop. counter0: By using this, the iteration of the loop starts from index 0.

Can I use while loop in Django?

The key point is that you can't do this kind of thing in the Django template language.

What does <UNK> Forloop counter print?

Q11:-What does {{ forloop. counter }} prints? It will not print if for loop variable is not defined. It will count the number of times loop ran.


2 Answers

You would use forloop.last. For example:

<ul> {% for item in menu_items %}     <li{% if forloop.last %} class='last'{% endif %}>{{ item }}</li> {% endfor %} </ul> 
like image 138
Paolo Bergantino Avatar answered Sep 22 '22 06:09

Paolo Bergantino


{{ forloop.last }}

like image 20
fuentesjr Avatar answered Sep 24 '22 06:09

fuentesjr