php 企业网站框架,品牌建设成效有待提升,自己做公司的网站吗,网站设置主页长期更新#xff0c;建议关注收藏点赞#xff01; 实例1 Jinja2 是 Flask 和 Django 使用的 模板引擎#xff0c;它允许你在 HTML 中嵌入 Python 代码#xff0c;以动态生成页面内容。Jinja2 语法类似于 Django 模板#xff0c;并支持变量、条件判断、循环、过滤器等。 fr…长期更新建议关注收藏点赞 实例1 Jinja2 是 Flask 和 Django 使用的 模板引擎它允许你在 HTML 中嵌入 Python 代码以动态生成页面内容。Jinja2 语法类似于 Django 模板并支持变量、条件判断、循环、过滤器等。 from flask import Flask, render_templateapp Flask(__name__)#app.route(/) 是路由装饰器定义访问时执行的函数这里即index。
app.route(/)
def index():return render_template(index.html)
#render_template()是Flask提供的函数用于加载HTML模板文件存放在templates 目录下。
#render_template(index.html) 让 Flask 查找 templates/index.html 并返回给浏览器。return render_template(index.html, title首页, message欢迎来到 Flask) #配合.html文件headtitle{{ title }}/title
/head
bodyh1{{ message }}/h1
/body
app.route(/)
def index():users [Alice, Bob, Charlie]return render_template(index.html, usersusers)
Jinja2 模板语法
{% ... %}表示 Jinja2 代码块里面可以写 Python 代码比如 for 循环、if 判断等。{% endfor %}结束循环ul{% for user in users %}li{{ user }}/li{% endfor %}
/ul
if __name__ __main__:app.run(debugTrue) # 启动 Flask 服务器