python - TypeError:“类”对象不可迭代-flask/sqlalchemy/jinja
问题描述
我正在从数据库中检索数据,并且想将其放入表中。我正在使用 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
解决方案
我找到了答案。
在我的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
推荐阅读
- jasmine - Jasmine UI 自动化
- ios - xCode 9.4.1 没有为团队找到具有 iTunes 连接访问权限的帐户
- python - 如何在 Python 中相互初始化的多个类之间正确共享命令行参数
- reactjs - 在reactjs中如何在选择下拉值时获取动态行
- python - 在 python 中使用 Stanford Corenlp for Chinese
- python - python pyautogui 与 pynput 一起工作
- php - 如何从图像 url 获取图像名称和图像扩展名
- ios - 如何在登录用户通过 Facebook 在 firebase for ios swift 中登录后检查?
- python - 如何在 SVM 中操作多维特征或使用多维特征训练模型?
- ios - 如果在类扩展中声明的任何方法都是静态的,我会感到困惑