从di打印值的Flask

2024-10-01 13:40:41 发布

您现在位置:Python中文网/ 问答频道 /正文

视图:

cur = g.db.execute('SELECT * FROM users WHERE username = "Bozo1"')
printme = [dict(id=row[0], username=row[1], password=row[2]) for row in cur.fetchall()]

模板:

^{pr2}$

印刷品:

[{'id': 1, 'username': 'Bozo1', 'password': 'abc123'}]

通常在Python中,我只需要执行print(printme[“id”]),它将输出1。但在烧瓶里,这种方法对我不起作用。在


Tags: from视图idexecutedbusernamepasswordwhere
1条回答
网友
1楼 · 发布于 2024-10-01 13:40:41

Normally in Python, I would just do print(printme["id"]) which would output 1.

不,你得到的是TypeError: list indices must be integers, not str,因为printmedictslist,而不是dict本身。假设要打印出所有返回的结果,则需要循环printme

{% for result in printme() %}
    {{ result['id'] }}
{% endfor %}

如果只想打印第一个,可以执行以下操作:

^{pr2}$

但是只获取一个结果会更有意义。在

相关问题 更多 >