count number of rows in flask templates count number of rows in flask templates flask flask

count number of rows in flask templates


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 templateeven we dont need to send another variable from views