Flask允许您将模板用于动态网页内容。使用模板的示例项目结构如下:
myproject/ /app/ /templates/ /index.html /views.py
views.py:
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def index():
pagetitle = "HomePage"
return render_template("index.html",
mytitle=pagetitle,
mycontent="Hello World")请注意,可以通过将键/值对附加到render_templates函数来将动态内容从路由处理程序传递到模板。在上面的示例中,“ pagetitle”和“ mycontent”变量将传递到模板以包含在呈现的页面中。将这些变量包括在模板中,并用双括号括起来:{{mytitle}}
index.html:
<html>
<head>
<title>{{ mytitle }}</title>
</head>
<body>
<p>{{ mycontent }}</p>
</body>
</html>与第一个示例相同地执行时,http://localhost:5000/标题将为“ HomePage”,段落的内容为“ Hello World”。