需要建立目錄templates;
把建好的html文件放到templates目錄里面;
約定好參數名字,單個名字可以直接使用;多參數使用字典傳遞;
樣例:
from flask import render_template
# 模板 (Templates)
#Flask 使用 Jinja2 模板引擎來渲染 HTML 模板。
# (把模版文件放在目錄templates里面)
# 模板允許你將 Python 代碼嵌入到 HTML 中,從而動態生成網頁內容。
@app.route('/hello/<name>')
def hello(name):return render_template('hello.html', name=name)#多參數模版
@app.route('/board')
def dashboard():context = {'title': '數據面板',"name":"test",'users': ['Alice', 'Bob', 'Charlie'],'stats': {'visits': 1500, 'sales': 89}}return render_template('hello.html', **context) # 字典解包傳遞?:ml-citation{ref="2,6" data="citationList"}
hello.html內容
<!DOCTYPE html>
<html>
<head><title>Template Test</title>
</head>
<body><h1>Hello, {{ name }}!</h1><h1>yourTitle, {{ title }}!</h1>
</body>
</html>
結果: