/app.py

""" flask 模板渲染,参数传递 """

# 引入Flask类,引入模板渲染方法
from flask import Flask, render_template

# 传入
app = Flask(__name__)

# 定义入口方法
@app.route('/')
def index():
    # return 'Hello!! Flask :)'
    """ 渲染index.html 并传入参数 title """
    return render_template('index.html',title = 'Flask')

# 运行
if __name__ == '__main__':
    app.run(debug=True, port=80, host='0.0.0.0')

# 开启debug, 设置端口号,设置主机名

/templates/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{ title }}</title>
</head>
<body>
     A page rendered by Flask with param 'title' = {{title}}
</body>
</html>

标签: none

分享到: