我在SQLite3中有一个表,我需要占用表中的几乎所有列并将它们输出到网页。对于我使用过烧瓶的其他页面,但这一直是将3或4个单值变量传递到模板中的情况。
我猜测它就像是将光标或更可能是cursor.fetchall()
调用中的行传递到模板中,然后在模板中循环行for? / p>
答案 0 :(得分:1)
应该是:
<table>
{% for item in items %}
<tr>
<td>{{column1}}</td>
<td>{{column2}}</td>
<td>{{column3}}</td>
<td>{{column4}}</td>
</tr>
{% endfor %}
</table>
答案 1 :(得分:0)
将您的数据作为某种集合传递,无论是the official tutorial中的字典,还是更简单的集合,如元组或列表。
是的,在您的模板中,您将使用{% for item in collection %} -> {% endfor %}
循环来渲染数据库中的所有数据。
示例Python方法:
@app.route('/print_items')
def print_items():
cursor = db.execute('SELECT column1,column2,column3,column4 FROM tablename')
return render_template('print_items.html', items=cursor.fetchall())
示例模板部分:
<table>
{% for item in items %}
<tr>
<td>column1</td>
<td>column2</td>
<td>column3</td>
<td>column4</td>
</td>
{% endfor %}
答案 2 :(得分:0)
您需要遵循以下条件:
[HTML]:
<table>
{% for item in items %}
<tr>
<td>{{item[0]}}</td>
<td>{{item[1]}}</td>
<td>{{item[2]}}</td>
<td>{{item[3]}}</td>
</td>
{% endfor %}
[python代码]:
cursor = db.execute('SELECT column1,column2,column3,column4 FROM tablename')
items = cursor.fetchall()
return render_template('print_items.html', items=items)