Python 在Flask中创建包含数据库值的HTML表
我想显示一个表,显示表中每个单位的三个字段的值。在从数据库创建字典和将对象传递给模板方面提供一些帮助将不胜感激Python 在Flask中创建包含数据库值的HTML表,python,flask,jinja2,Python,Flask,Jinja2,我想显示一个表,显示表中每个单位的三个字段的值。在从数据库创建字典和将对象传递给模板方面提供一些帮助将不胜感激 @app.route('/') def index(row): unit_count = Units.query.count() unit = Units.query.filter_by(id=row).first() rows = [] # Unsure how to define rows from Units db table return ren
@app.route('/')
def index(row):
unit_count = Units.query.count()
unit = Units.query.filter_by(id=row).first()
rows = [] # Unsure how to define rows from Units db table
return render_template('table_overview.html', title='Overview', rows=rows, unit=unit)
{%用于行中的行%}
{{row.unit.idnum}
{{row.unit.product_code}
{{row.unit.unit_status}
{%endfor%}
首先,查看功能无法接收指定的行
输入。如果要显示表中的所有行,可以这样做:
views.py:
表_overview.html(模板)
{%用于行中的行%}
{{row.idnum}}
{{row.product_code}}
{{row.unit_status}}
{%endfor%}
成功了!比我想象的要简单得多。非常感谢你对我的帮助,我真的很感激!谢谢这肯定比用纯Python脚本生成html要好;)烧瓶已经回应了我的祈祷。
@app.route('/')
def index():
rows = Units.query.all()
return render_template('table_overview.html',
title='Overview',
rows=rows)
{% for row in rows %}
<tr>
<td>{{ row.idnum }}</a></td>
<td>{{ row.product_code }}</td>
<td bgcolor="{{ row.stat_colour }}">{{ row.unit_status }}</td>
</tr>
{% endfor %}