python - 无法在 html 页面中显示来自 mysql db 的数据
问题描述
这是路由代码:
@app.route("/notice_disp" , methods=['POST','GET'])
def notice_disp():
cur=mysql.connection.cursor()
result=cur.execute("SELECT * FROM notices")
if result > 0:
data=cur.fetchall()
cur.close()
return render_template('notice_disp.html',data = data )
这是html代码:
<table>
<thead>
<tr>
<th>notice id</th>
<th>notice name</th>
<th>notice</th>
</tr>
</thead>
<tbody>
{% for row in data %}
<tr>
<td>{{row.0}}</td>
<td>{{row.1}}</td>
<td>{{row.2}}</td>
</tr>
{% endfor %}
</tbody>
</table>
html 页面中的输出: 无法显示数据,我将数据存储在 db 中
解决方案
fetchall()
返回一个元组列表。因此,您必须使用方括号来访问每个元组的元素:
{% for row in data %}
<tr>
<td>{{ row[0] }}</td>
<td>{{ row[1] }}</td>
<td>{{ row[2] }}</td>
</tr>
{% endfor %}
推荐阅读
- django - Django Rest FrameWork 使用 group by 减少查询次数
- reactjs - 如何使用 JEST 模拟 Axios 服务包装器
- php - 在 wordpress 中使用 PHP 从动态下载 URL 获取文件
- sql - SQL 错误(4104):无法绑定多部分标识符“t1.ENTRY_DATETIME”
- python - PyCharm:按组合键搜索键盘映射?
- javascript - React 中的悬停功能
- javascript - 如何使用 jQuery 制作高级过滤器
- c++ - 如何从用户那里打印温度表?
- eclipse - 如何在 Java servlet/Tomcat 中接收 webhook 帖子?
- node.js - `openUri()` 的 `uri` 参数必须是字符串,得到“未定义”。确保 `mongoose.connect()` 的第一个参数是一个字符串