视图:
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。但在烧瓶里,这种方法对我不起作用。在
不,你得到的是
TypeError: list indices must be integers, not str
,因为printme
是dicts
的list
,而不是dict
本身。假设要打印出所有返回的结果,则需要循环printme
:如果只想打印第一个,可以执行以下操作:
^{pr2}$但是只获取一个结果会更有意义。在
相关问题 更多 >
编程相关推荐