Flask文档中对此进行了非常清楚的解释,因此建议您阅读此书以全面理解,但这是呈现模板变量的非常简单的示例。
HTML模板文件存储在
templates/index.html:
<html><body> <p>Here is my variable: {{ variable }}</p></body></html>和简单的Flask应用程序:
from flask import Flask, render_templateapp = Flask('testapp')@app.route('/')def index(): return render_template('index.html', variable='12345')if __name__ == '__main__': app.run()运行此脚本,然后在浏览器中访问http://127.0.0.1:5000/。您应该看到的
variable渲染值为
12345



