[英]Why does jinja isn't working for simple template
I am trying to create html document from html-template and python我正在尝试从 html-template 和 python 创建 html 文档
Html template html模板
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=utf-8">
<meta name=Generator content="Microsoft Word 15 (filtered)">
<title>Договор подряда № 8/003-07 на выполнение проектных работ</title>
<style>
</style>
</head>
<body bgcolor=white lang=RU>
<div class=WordSection1>
<p class=MsoNormal align=center style='margin-right:-1.15pt;text-align:center'><b><span
style='font-size:12.0pt'>Договор подряда № {{document_number}}</span></b></p>
</div>
</body>
</html>
I want to fill **document_number ** but instead of value i have empty line "" How can I fix it?我想填写 **document_number ** 但我有空行“”而不是值我该如何解决?
My code我的代码
import jinja2 # create html files
templateLoader = jinja2.FileSystemLoader(
searchpath="./"
)
data_to_render = {
"document_number": "123",
"building_object_address": "Moscow"
}
#templateEnv = jinja2.Environment(loader=templateLoader)
templateEnv = jinja2.Environment(
loader=templateLoader,
comment_start_string='{=',
comment_end_string='=}',
)
template = templateEnv.get_template("income_contract_short.html")
sourceHtml = template.render(json_data=data_to_render)
You're passing your data to template.render as json_data
, so the only variable available to the template is json_data
, which you have to access like a dictionary.您将数据作为json_data
传递给 template.render ,因此模板唯一可用的变量是json_data
,您必须像字典一样访问它。
Option 1:选项1:
In the template, replace {{document_number}}
with {{json_data['document_number']}}
在模板中,将{{document_number}}
替换为{{json_data['document_number']}}
Option 2:选项 2:
Pass your data elements as separate keyword arguments to template.render, changing:将您的数据元素作为单独的关键字参数传递给 template.render,更改:
sourceHtml = template.render(json_data=data_to_render)
to至sourceHtml = template.render(document_number=data_to_render["document_number"], address=data_to_render["building_object_address"])
You could then access the building_object_address data as simply {{address}}.然后,您可以简单地访问 building_object_address 数据 {{address}}。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.