flask - 如何 query/iterate 超过 2 个单独的 bootstrap 表
flask - How to query/iterate over 2 seperate bootstrap tables
- 我在 postgres table 中有 2 个 table A 和 B(它们在列名和列数方面完全相同)
- 我在 bootstrap
中有 2 个 table
- 我想查询从 postgres table A 到 table A of (bootstrap)
- 我想查询从 postgres table B 到 table B of (bootstrap)
假设我想使用 python 列表
完成此操作
aList = ['A', 'B']
for network in aList:
db.session.execute("SELECT * from booTable"+network+" ")
So in python/jinja 如何进行上述查询,如果是网络 A,则显示在 table A 中 index.html,如果是网络 B,则将其显示在 table B.
目前我拥有的是:
@app.route('/')
def Index():
tableA = db.session.execute("SELECT * from booTableA").fetchall()
tableB = db.session.execute("SELECT * from booTableB").fetchall()
return render_template("index.html", tableA=tableA, tableB=tableB)
在 index.html 中,这就是我所拥有的:
<table class="table">
{% for a in tableA %}
<tr>
<td>{{a.name}}</td>
<td>{{a.age}}</td>
</tr>
</table>
<table class="table">
{% for b in tableB%}
<tr>
<td>{{b.name}}</td>
<td>{{b.age}}</td>
</tr>
</table>
如果有多个 table 的一个执行查询来完成上述任务会很棒,因为如果我有多个 network/table 那么我可以在末尾添加“+network+”而不是执行多个select 查询每个 network/table 。任何帮助实现上述问题的帮助将不胜感激。
这是一种方法:
from collections import OrderedDict
aList = ['A', 'B']
results = OrderedDict()
for network in aList:
ret_ = db.session.execute("SELECT * from booTable"+network+" ")
results[network] = ret_
然后在你的神社中:
{% for key, values in results.items() %}
<table class="table">
{% for v in values %}
{% endfor %}
</table>
{% endfor %}
这将允许您根据网络密钥构建您的 table,然后循环遍历项目。
免责声明:我脑子里写下的代码,如果您复制和粘贴可能无法运行。放异常处理和检查就看你的了
- 我在 postgres table 中有 2 个 table A 和 B(它们在列名和列数方面完全相同)
- 我在 bootstrap 中有 2 个 table
- 我想查询从 postgres table A 到 table A of (bootstrap)
- 我想查询从 postgres table B 到 table B of (bootstrap)
假设我想使用 python 列表
完成此操作
aList = ['A', 'B']
for network in aList:
db.session.execute("SELECT * from booTable"+network+" ")
So in python/jinja 如何进行上述查询,如果是网络 A,则显示在 table A 中 index.html,如果是网络 B,则将其显示在 table B.
目前我拥有的是:
@app.route('/')
def Index():
tableA = db.session.execute("SELECT * from booTableA").fetchall()
tableB = db.session.execute("SELECT * from booTableB").fetchall()
return render_template("index.html", tableA=tableA, tableB=tableB)
在 index.html 中,这就是我所拥有的:
<table class="table">
{% for a in tableA %}
<tr>
<td>{{a.name}}</td>
<td>{{a.age}}</td>
</tr>
</table>
<table class="table">
{% for b in tableB%}
<tr>
<td>{{b.name}}</td>
<td>{{b.age}}</td>
</tr>
</table>
如果有多个 table 的一个执行查询来完成上述任务会很棒,因为如果我有多个 network/table 那么我可以在末尾添加“+network+”而不是执行多个select 查询每个 network/table 。任何帮助实现上述问题的帮助将不胜感激。
这是一种方法:
from collections import OrderedDict
aList = ['A', 'B']
results = OrderedDict()
for network in aList:
ret_ = db.session.execute("SELECT * from booTable"+network+" ")
results[network] = ret_
然后在你的神社中:
{% for key, values in results.items() %}
<table class="table">
{% for v in values %}
{% endfor %}
</table>
{% endfor %}
这将允许您根据网络密钥构建您的 table,然后循环遍历项目。
免责声明:我脑子里写下的代码,如果您复制和粘贴可能无法运行。放异常处理和检查就看你的了