【发布时间】:2016-10-18 16:44:03
【问题描述】:
我有一个 Mongo 数据库,我想编写一个脚本来执行查询并将结果传递给一个 html 文件。
这个呈现的 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
我将不胜感激。
【问题讨论】:
标签: python mongodb python-2.7 jinja2 pymongo