The*_*ify 3 python arrays list jinja2 flask
我尝试在网页上制作一些带有数据导入的表格!我正在使用 Flask-Jinja 和 Python。例如我有这个双重列表:
list = [[{'pricebb': 1199.99, 'model': 'model1', 'pricebh': 1199, 'pricea': 1299}, {'pricebb': 1199.99, 'model': 'model2', 'pricebh': 1199, 'pricea': 1299}, {'pricebb': 1499.99, 'model': 'model3', 'pricebh': 1499, 'pricea': 1599}], [{'pricebb': 399.99, 'model': 'model4', 'pricebh': 459, 'pricea': 499}, {'pricebb': 599.99, 'model': 'model5', 'pricebh': 669, 'pricea': 699}, {'pricebb': None, 'model': 'model6', 'pricebh': 899, 'pricea': 999}]]
Run Code Online (Sandbox Code Playgroud)
我想用循环将其分开并在一页上创建 2 个不同的表。如果我将 Python 与 list[0] 一起使用,我会得到第一个子列表,但是当我在 Flask 上尝试时:
{% for post in posts %}
<p>{{post[0]}}</p>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
它返回给我 - model1 和 model4)为什么会发生?如何从列表中获取第一个子列表?你有什么想法吗!?谢谢你!
run1()
list= sqlselect()# here is array from DB
a = list # its a list sample that I posted above
@FlaskApp2.route('/', methods=('Get', 'Post'))
@FlaskApp2.route('/index')
def index():
user = {'nickname': 'parser'} # fake user
return render_template("index.html",
title='Web',
user=user,
posts=list, describe=des)
Run Code Online (Sandbox Code Playgroud)
这是index.html:
<table>
<caption>products compare list ({{item}})</caption>
<thead>
<tr>
<th>qqq.com</th>
<th>Price</th>
<th>qqq.com</th>
<th>qqq.com</th>
</tr>
</thead>
{% for post in posts %}
<p>{{post[0]}}</p>
{% endfor %}
</table>
Run Code Online (Sandbox Code Playgroud)
您可以通过posts[0]- 第一个列表和posts[1]- 第二个列表访问内部列表(您永远不应该在 python 中使用保留字命名变量 - 不要命名您的 var list,但是mylist)。因此,要访问第一个列表的元素,您应该使用:
{% for post in posts[0] %}
{% for key, value in post.items %}
<p>{{ key }}: {{ value }}</p>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
对于第二个列表:
{% for post in posts[1] %}
{% for key, value in post.items %}
<p>{{ key }}: {{ value }}</p>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)