我在模板中写了个 for 循环,类似 {% for n in Data %}
这样的,然后我想看 flask 把这个 for 循环展开后最终生成的模板文件,应该怎么去查看? 我不要通过浏览器查看源代码的方式。
我在模板中写了个 for 循环,类似 {% for n in Data %}
这样的,然后我想看 flask 把这个 for 循环展开后最终生成的模板文件,应该怎么去查看? 我不要通过浏览器查看源代码的方式。
使用 render_template_string
即可, 传参方式和 render_template
类似,只是第一个参数为读取的模板内容, 而不是模板文件路径
示例代码如下(简短演示,就不写路由, 直接用 app_context 模拟访问):
from flask import (
Flask,
render_template_string
)
app = Flask(__name__)
tpl_args = {
'Data': [1, 2, 3, 4, 5]
}
with app.app_context():
result = render_template_string('{% for n in Data %} {{ n }} {% endfor %}', **tpl_args)
print(result)
输出如下:
4 回答4.4k 阅读✓ 已解决
4 回答3.8k 阅读✓ 已解决
1 回答3k 阅读✓ 已解决
3 回答2.1k 阅读✓ 已解决
1 回答4.5k 阅读✓ 已解决
1 回答3.8k 阅读✓ 已解决
1 回答2.8k 阅读✓ 已解决
flask 使用jinja模块,直接调用就可以了, 如下
参考:http://docs.jinkan.org/docs/j...