6、模板引擎简单使用

在根路径下创建 templates 文件夹,该文件夹用于放置模板文件

1、创建首页模板文件 index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>这是首页</title>
</head>
<body>
    <h1>这是首页!</h1>
</body>
</html>

2、创建用户模板文件 user.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>这是用户中心</title>
</head>
<body>
    <h1>这是用户中心! </h1>
</body>
</html>

3、在根文件夹创建主程序文件

from flask import Flask
from flask import render_template
app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')  # 跳转至模板文件

@app.route('/user/')
def user():
    return render_template('user.html')

if __name__ == '__main__':
    app.run(debug=True)

参考资料

https://weread.qq.com/web/reader/0a932660718ac6bc0a9702e

posted @ 2022-05-29 17:04  tiansz  阅读(21)  评论(0编辑  收藏  举报