在 Flask 中,您可以使用 render_template()
函数来渲染模板。该函数的第一个参数是模板的名称,它是一个字符串。例如,假设您有一个名为 index.html
的模板,您可以使用以下代码来渲染它:
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
在这种情况下,Flask 会在您的应用程序的 templates
文件夹中查找模板。如果您希望 Flask 在其他位置查找模板,您可以使用 render_template()
函数的第二个参数,该参数是模板所在文件夹的路径。例如,假设您有一个名为 templates
的文件夹,该文件夹位于应用程序的根目录下,并且您希望查找名为 index.html
的模板,您可以使用以下代码:
return render_template('index.html', template_folder='templates')
希望这能帮到您!