gpt4 book ai didi

Python Bottle 模板问题 : AttributeError ("' dict' object has no attribute 'city' ", )

转载 作者:可可西里 更新时间:2023-11-01 09:56:28 25 4
gpt4 key购买 nike

作为一个学习项目,我将 MongoDB 与 Bottle 一起用于 Web 服务。我想要做的是从 MongoDB 获取结果并将它们显示在模板中。这是我想要的模板输出:

output.tpl
<html><body>
%for record in records:
<li>{{record.city}} {{record.date}}
%end
</body></html>

我可以毫无问题地提取数据:

result = db.records.find(query).limit(3)
return template('records_template', records=result)

但这导致根本没有输出 - 一些调试显示结果是某种光标:

<pymongo.cursor.Cursor object at 0x1560dd0>

所以我试图将其转换成模板想要的东西:

result = db.records.find(query).limit(3)
viewmodel=[]
for row in result:
l = dict()
for column in row:
l[str(column)]=row[column]
viewmodel.append(l)
return template('records_template', records=viewmodel)

调试显示我的 View 数据看起来正常:

[{'_id': ObjectId('4fe3dfbc62933a0338000001'),
'city': u'CityName',
'date': u'Thursday June 21, 2012'},
{'_id': ObjectId('4fe3dfbd62933a0338000088')
'city': u'CityName',
'date': u'Thursday June 21, 2012'},
{'_id': ObjectId('4fe3dfbd62933a0338000089')
'city': u'CityName',
'date': u'Thursday June 21, 2012'}]

但这就是我得到的回应。有什么想法吗?

AttributeError("'dict' object has no attribute 'city'",)

编辑:我添加了关于 l[str(column)]=row[column] 的一点,以将字典键转换为非 unicode 字符串,以防出现问题,但这两种方式似乎都不重要。

最佳答案

您需要使用字典语法来查找属性:

{{record['city']}} {{record['date']}}

关于Python Bottle 模板问题 : AttributeError ("' dict' object has no attribute 'city' ", ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11166763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com