gpt4 book ai didi

python - 如何将多个模板传递给 flask.render_template()

转载 作者:太空宇宙 更新时间:2023-11-04 08:45:01 26 4
gpt4 key购买 nike

我正在尝试部署一个 flask 应用程序,我想要一个带有 html 文件列表的 flask.render_template() 方法。在这里,我看到它是合格的。 http://flask.pocoo.org/docs/0.12/api/#flask.render_template

我正在尝试使用这段代码

from flask import Flask, render_template
app = Flask(__name__)
app.debug = True

@app.route('/')
def hello():
templs = ["_header.html", "_footer.html"]
return render_template(templs)

if __name__== '__main__':
app.run()

但实际上服务器只返回列表中的第一个模板。如何遍历此列表以呈现列表中的所有模板?

谢谢,亚历克斯

最佳答案

据我所知,您正在尝试呈现静态页眉和页脚。我建议准备类似“layout.html”的内容,其中包含页眉和页脚:

//layout.html
<html>
<head>//headhere</head>
<header>//your static header</header>
<main>
{% block body %}
//content will be here
{% endblock %}
</main>
<footer> //your static footer </footer>
</html>

然后在“子”模板中(例如:index.html)使用:

//index.html
{% extends "layout.html" %}
{% block body %}
//your code here
{% endblock %}

它将从 layout.html 渲染页眉和页脚,从 index.html 渲染 rest。

关于python - 如何将多个模板传递给 flask.render_template(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41483315/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com