【问题标题】:Having problems with the usage of a block in Flask在 Flask 中使用块时遇到问题
【发布时间】:2021-01-18 10:49:41
【问题描述】:

所以我尝试建立一个(小)网站(用于学习目的)并且在使用块时遇到了一些问题。 我觉得问题很明显,但我无法弄清楚。 这是我的代码:

# main.py
from flask import Flask, render_template

app = Flask(__name__)

@app.route("/")
def home():
    return render_template("main.html")

if __name__ == "__main__":
    app.run(debug=True)
# main.html
<!DOCTYPE html>
<head>
    <title>
        {% block title %}{% endblock %}
    </title>
</head>
<body>
    {% block content %}{% endblock %}
</body>
</html>
# mainexdended.html
{% extends "main.html" %}
{% block title %}Test{% endblock %}

{% block content %} 
<h1> Content Test </h1>
{% endblock %}

现在,当我运行代码时,我只是得到一个空白页。 mainextended.html 中定义的所有内容都会被忽略。 我该如何改变呢?

【问题讨论】:

    标签: python flask block


    【解决方案1】:

    那是因为您正在渲染 main.html,而不是 mainextended.html。

    你需要改变

    @app.route("/")
    def home():
        return render_template("mainextended.html")
    

    你应该看到 main.html 和 mainextended.html 的内容

    如果您使用“扩展”,您总是希望呈现“扩展”的 html,还有一个包含内容的功能,它可以反过来工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-06
      • 1970-01-01
      • 2013-06-14
      • 1970-01-01
      • 2020-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多