Flask 模板继承不起作用,输出继承命令而不是子代

时间:2021-01-17 08:06:58

标签: python flask

enter image description here

然而,我期待上图的输出。我已收到作为输出。enter image description here

基本代码或布局代码根本没有在这里继承子“home”并按原样显示命令。

<!DOCTYPE html>
<html>
  <head>
      <title>Flask App</title>
      <link rel="stylesheet" href="{{url_for('static',filename='css/main.css')}}">
  </head>
  <body>
    <header>
      <div class="container">
        <h1 class="logo">Ardit's web app</h1>
        <strong><nav>
          <ul class="menu">
            <li><a href="{{ url_for('home') }}">Home</a></li>
            <li><a href="{{ url_for('about') }}">About</a></li>
          </ul>
        </nav></strong>
      </div>
    </header>
    <div class="container">
        {%block content%}
        {%endblock%}
    </div>
  </body>
</html>

以上代码为布局代码。

{% extends "layout.html" %}
{% block content %}
<div class="home">
    <h1>My homepage</h1>
    <p>This is a test website</p>
</div>
{% endblock %}

这是用于主页或孩子的。输出屏幕截图用于此。即使对于布局页面,它也直接打印出 {%block content%} {%endblock%}。

我在它的文档页面上看到过flask示例,语法是相同的https://flask.palletsprojects.com/en/1.1.x/patterns/templateinheritance/

我使用的python脚本是

from flask import Flask, render_template
import jinja2
#FLask class object from flas

#initializing the class 
app=Flask(__name__)


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

@app.route('/about/')
def about():
    return render_template("about.html")


if __name__=="__main__":
    app.run(debug=True)
    

1 个答案:

答案 0 :(得分:1)

尝试更改这些行:

{%block content%}
{%endblock%}

为此:

{% block content %}
{% endblock %}

可能是这些空间 after-before % 导致了这个问题 在任何情况下,您都必须在您的应用程序中试用这些 html 文件,不仅通过浏览器将它们作为简单的 html 文件打开,而且以最新的方式它们将始终与代码一起显示,而不是内容

相关问题