简体   繁体   English

用jinja2、html和flask构建动态表

[英]Building a dynamic table with jinja2, html and flask

<table style="width:100%", border="1">
        {% for item in items %}    
        <tr>               
            <td>{{Description[item]}}</td>
            <td>{{Location[item]}}</td>
            <td>{{Status[item]}}</td>                
        </tr>
        {% endfor %}    
</table>

I am trying to create a table using this for loop, the variables are being passed through in the flask framework, 'items' will always be the same length as the three lists (Description, location and status).我正在尝试使用此 for 循环创建一个表,变量正在烧瓶框架中传递,“项目”将始终与三个列表(描述、位置和状态)的长度相同。 I am aware of the question below: How to build up a HTML table with a simple for loop in Jinja2?我知道以下问题: 如何在 Jinja2 中使用简单的 for 循环构建 HTML 表?

but I can not see how my code here differs to the working answer to this question, is it because I am using a list instead of a dictionary ?但我看不出我的代码与这个问题的工作答案有何不同,是因为我使用的是列表而不是字典吗?

This is the flask framework, where the lists are created an passed through using render template:这是烧瓶框架,其中列表是使用渲染模板创建的:

def index():
description = []
location = []
status = []
imix80m = ''
imix = ''
with open('behavepretty.json') as a:
    data = json.load(a)
    for n in range(0,len(data)):
        i = 0
        for i in range(0, len(data[n]['elements'])):
            x = data[n]['elements'][i]['name']
            description.append(x)
            y = data[n]['elements'][i]['location']
            location.append(y)
            z = data[n]['elements'][i]['status']
            status.append(z)
        n = 0
    for n in range(0,len(data)):
        if n == 0:
            imix80m = data[n]['status']
        elif n == 1:
            imix = data[n]['status']
a.close()
return render_template('trial.html', Description = description, Location= location, Status = status, result1 = imix80m, result2 = imix, jfile = data, items = description)

You just need a loop counter:你只需要一个循环计数器:

<table style="width:100%", border="1">
        {% for item in Description %}    
        <tr>               
            <td>{{Description[loop.index0 ]}}</td>
            <td>{{Location[loop.index0]}}</td>
            <td>{{Status[loop.index0]}}</td>                            
        </tr>
        {% endfor %}    
</table>

Or, you could pack the 3 into a list of lists:或者,您可以将 3 打包到列表列表中:

my_list = [[Description0, Location0, Status0], [Description1, Location1, Status1], ...]

Then:然后:

    {% for item in my_list %}    
        <tr>               
            <td>{{ item[0] }}</td>
            <td>{{ item[1] }}</td>
            <td>{{ item[2] }}</td>                            
        </tr>
    {% endfor %} 

Or, more robustly, a list of dictionaries:或者,更可靠的是,字典列表:

my_list = [
    {
        "description" : xxx, 
        "location" : yyy, 
        "status" : zzz
    },
    {
        "description" : www, 
        "location" : eee, 
        "status" : rrr
    },
    ...
]

Then:然后:

    {% for item in my_list %}    
        <tr>               
            <td>{{ item.description }}</td>
            <td>{{ item.location }}</td>
            <td>{{ item.status }}</td>                            
        </tr>
    {% endfor %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM