【发布时间】:2021-07-28 18:08:24
【问题描述】:
我有一个咖啡豆模型,我将它发送到我的HTML 页面并显示对象的数据。目前,我有一个解决方案,如果我修改我的Bean 对象,我需要更新我的HTML 代码。
我想通过迭代与对象关联的属性而不是将每个属性硬编码到HTML 中来从我的程序中移除耦合。
我当前(过时的)解决方案:
# Bean model defined using Flask-SQLAlchemy
class Bean(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(20), index=True, nullable=False)
...
<!-- some_page.html -->
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
...
</tr>
</thead>
<tbody>
{% for bean in beans %}
{% include '_bean_info.html' %}
{% endfor %}
</tbody>
</table>
<!-- _bean_info.html -->
<tr>
<th scope="col">{{ bean.id }}</th>
<th scope="col">{{ bean.name }}</th>
...
</tr>
我想做什么:
<!-- _bean_info.html -->
<tr>
{% for each b in bean %}
<th scope="col">
{{ bean.b }} <!-- where 'b' is an attribute, e.g. 'name' -->
</th>
</tr>
【问题讨论】:
-
您的
bean模型是如何定义的?
标签: flask jinja2 flask-sqlalchemy