我正在從資料庫中檢索資料,我想將其放入表中。我正在使用 SQLAlchemy 和 SQLite 資料庫。
這是我在 routes.py 中的代碼:
headings = ('Name', 'Code')
classes = tuple(list(ClassName.query.filter_by(id=current_user.id).all()))
print(classes)
return render_template("view.html", headings=headings, classes=classes)
這是 view.html 中的代碼:
<div class="view-table">
<table>
<tr>
{% for header in headings %}
<th>{{ header }}</th>
{% endfor %}
</tr>
{% for row in classes %}
<tr>
{% for cell in row %}
<td>{{ cell }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
</div>
每當我運行它時,我都會收到此錯誤:
TypeError: 'Class' object is not iterable
我正在關注本教程,這就是我將串列更改為元組的原因:https : //youtu.be/mCy52I4exTU
uj5u.com熱心網友回復:
我找到了這個問題的答案。
在我的routes.py,我這樣做了:
classes = Classes.query.filter_by(classAdminID=current_user.id).all()
在我的view.html,我這樣做了:
<div class="view-table">
<table>
<tr>
{% for header in headings %}
<th>{{ header }}</th>
{% endfor %}
</tr>
{% for row in classes %}
<tr>
<td>{{ row.Name }}</td>
<td>{{ row.Code }}</td>
</tr>
{% endfor %}
</table>
</div>
我在這個視頻中找到了解決方案:https : //youtu.be/hbDRTZarMUw
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/360102.html
