i have send a variable from my views to templates which consist of the data from database
this is what i am using in my template
{% for i in data %}
<tr>
<td>{{i.id}}</td>
<td>{{i.first_name}}</td>
<td>{{i.last_name}}</td>
<td>{{i.email}}</td>
</tr>
{% endfor %}
there are seven entries in this loop , i need to show count lease suggest how can i do this
Inside the loop you can access a special variable called loop
and you can see the number of items with {{ loop.length }}
This is all you can do with loop auxiliary variable:
loop.index The current iteration of the loop. (1 indexed)
loop.index0 The current iteration of the loop. (0 indexed)
loop.revindex The number of iterations from the end of the loop (1 indexed)
loop.revindex0 The number of iterations from the end of the loop (0 indexed)
loop.first True if first iteration.
loop.last True if last iteration.
loop.length The number of items in the sequence.
loop.cycle A helper function to cycle between a list of sequences. See the explanation below.
loop.depth Indicates how deep in deep in a recursive loop the rendering currently is. Starts at level 1
loop.depth0 Indicates how deep in deep in a recursive loop the rendering currently is. Starts at level 0
EDIT:
To see the count of items outside de for loop you can generate another variable from your view like count_data = len(data)
or you can use the length
filter:
Data count is {{ data|length }}:
{% for i in data %}
<tr>
<td>{{i.id}}</td>
<td>{{i.first_name}}</td>
<td>{{i.last_name}}</td>
<td>{{i.email}}</td>
</tr>
{% endfor %}
{{ data|length }}
this works perfect we not need to use this in loop just use any where in the template even we dont need to send another variable from views
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With