joh*_*n2x 9 python jinja2 flask
假设我有一堆常量的settings.py文件(未来可能更多).如何在Jinja模板中访问这些变量?
你需要定义一个context_processor:
@app.context_processor
def inject_globals():
return dict(
const1 = const1,
const2 = const2,
)
Run Code Online (Sandbox Code Playgroud)
以这种方式注入的值将直接在模板中提供:
<p>The values of const1 is {{ const1 }}.</p>
Run Code Online (Sandbox Code Playgroud)
您可能希望使用Python dir函数来避免列出所有常量.
| 归档时间: |
|
| 查看次数: |
5906 次 |
| 最近记录: |