0
我有一个Mongo数据库,我想做一个脚本来执行查询并将结果传递给一个html文件。如何将Mongo光标对象传递给Jinja模板?
此呈现的HTML文件将被用作电子邮件,我将发送给我的同事的HTML正文。
但是模板未显示,这里是我的本钱
我的HTML看起来像这样:
<table>
{ %for q in query %}
<tr>
<td>{{ q['containers'] }} </td>
<td>{{ q['cases'] }} </td>
<td>{{ q['gross_weight'] }} </td>
<td>{{ q['volume'] }} </td>
</tr>
{ %endfor% }
</table>
虽然我的剧本是这样的:
from pymongo import MongoClient, ASCENDING
from jinja2 import Template
def main():
client = MongoClient()
collection = client.supplyChain['commonRegimes']
parameters = {
'delivery_to_warehouse':None,
'regime':{'$in':['10', '91']}
}
query_result = collection.find(parameters).sort('eta_warehouse', ASCENDING)
templatefile = open('D:/myScripts/ccreport/reports/templates/nextArrivals.html').read()
template = Template(templatefile)
message = template.render(query = query_result)
if __name__ == '__main__':
main()
我得到的错误:
jinja2.exceptions.UndefinedError: 'q' is undefined
我将不胜感激任何帮助。