欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 资讯 > 【后端】Flask

【后端】Flask

2025/2/4 13:34:21 来源:https://blog.csdn.net/qq_41775119/article/details/145392236  浏览:    关键词:【后端】Flask

长期更新,建议关注收藏点赞!


实例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文件
'''
<head><title>{{ title }}</title>
</head>
<body><h1>{{ message }}</h1>
</body>
'''@app.route('/')
def index():users = ["Alice", "Bob", "Charlie"]return render_template("index.html", users=users)
'''Jinja2 模板语法
{% ... %}:表示 Jinja2 代码块,里面可以写 Python 代码,比如 for 循环、if 判断等。{% endfor %}结束循环<ul>{% for user in users %}<li>{{ user }}</li>{% endfor %}
</ul>
'''if __name__ == "__main__":app.run(debug=True)  # 启动 Flask 服务器

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com