计算烧瓶模板中的行数 [英] count number of rows in flask templates

查看:24
本文介绍了计算烧瓶模板中的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将一个变量从我的视图发送到由数据库中的数据组成的模板

i have send a variable from my views to templates which consist of the data from database

这是我在模板中使用的

{% 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

推荐答案

在循环内部你可以访问一个叫做 loop 的特殊变量,你可以用 {{ loop 查看项目的数量.length }}

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 循环的当前迭代.(1 个已编入索引)

  • loop.index The current iteration of the loop. (1 indexed)

loop.index0 循环的当前迭代.(0 个索引)

loop.index0 The current iteration of the loop. (0 indexed)

loop.revindex 从循环结束开始的迭代次数(1 个索引)

loop.revindex The number of iterations from the end of the loop (1 indexed)

loop.revindex0 从循环结束开始的迭代次数(0 索引)

loop.revindex0 The number of iterations from the end of the loop (0 indexed)

loop.first 如果第一次迭代为真.

loop.first True if first iteration.

loop.last 如果最后一次迭代为真.

loop.last True if last iteration.

loop.length 序列中的项目数.

loop.cycle 在序列列表之间循环的辅助函数.请参阅下面的说明.

loop.cycle A helper function to cycle between a list of sequences. See the explanation below.

loop.depth 表示当前渲染在递归循环中的深度.从级别 1 开始

loop.depth Indicates how deep in deep in a recursive loop the rendering currently is. Starts at level 1

loop.depth0 表示当前渲染在递归循环中的深度.从 0 级开始

loop.depth0 Indicates how deep in deep in a recursive loop the rendering currently is. Starts at level 0

要查看 de for 循环之外的项目数,您可以从视图中生成另一个变量,例如 count_data = len(data),或者您可以使用 length 过滤器:

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 %}

这篇关于计算烧瓶模板中的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆