需要建立目录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>
结果: