Python 使用jinja2、html和flask构建动态表
您只需要一个循环计数器:Python 使用jinja2、html和flask构建动态表,python,html,flask,jinja2,Python,Html,Flask,Jinja2,您只需要一个循环计数器: 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'])
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)
然后:
然后:
{%for my_list%中的项目]
{{item.description}}
{{item.location}
{{item.status}
{%endfor%}
发布视图代码(python)。是否定义了说明
,位置
,状态
?说明、位置和状态都是从flask框架传递的单独列表,项目只是用作计数器,我是否应该将三个列表合并到一个字典中?什么是项目
?那是你要通过的名单吗?也许你只是在找一个循环计数器。我将编辑答案,但您需要发布python代码发布python,是的,我只是将其用作循环计数器需要在某个地方定义'forloop.counter',或者这是内置的识别吗?jinja2.exceptions.UndefinedError:'forloop'未定义,收到此错误消息@GAEfan
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)
<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>
my_list = [[Description0, Location0, Status0], [Description1, Location1, Status1], ...]
{% for item in my_list %}
<tr>
<td>{{ my_list[0] }}</td>
<td>{{ my_list[1] }}</td>
<td>{{ my_list[2] }}</td>
</tr>
{% endfor %}
my_list = [
{
"description" : xxx,
"location" : yyy,
"status" : zzz
},
{
"description" : www,
"location" : eee,
"status" : rrr
},
...
]
{% for item in my_list %}
<tr>
<td>{{ item.description }}</td>
<td>{{ item.location }}</td>
<td>{{ item.status }}</td>
</tr>
{% endfor %}