-
目录结构
project/ # 工程目录 manage.py # 启动控制文件 templates/ # 模板文件目录
-
模板渲染
-
在
templates
目录下创建一个模板文件index.html
,在其中写入页面内容 -
在视图函数中渲染模板:
render_template('index.html')
-
渲染模板字符串:
render_template_string('<h1>渲染模板字符串</h1>')
-
设置模板文件自动加载:
app.config['TEMPLATES_AUTO_RELOAD'] = True
,调试模式会自动加载
-
-
使用变量
-
需要解析的变量需要放在
{ { }}
中 -
渲染模板文件时需要传递相关变量:
render_template('var.html', name='goudan')
-
注释写在
{# #}
中
-
-
使用过滤器
过滤器 说明 upper 全大写 lower 全小写 title 每个单词首字母大写
Jinja2语法
最新推荐文章于 2024-06-16 15:50:56 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)