【问题标题】:unable to render child template to layout无法将子模板渲染到布局
【发布时间】:2018-11-09 21:14:49
【问题描述】:

我尝试渲染render_template('index.html') 和render_template('layout.html')。只有标题和布局呈现。索引未呈现。哪里出了问题?

app.py

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



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

header.html:

 {% block header %}
   <div>this is header</div>  
 {% endblock %}

布局.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>{% block title %} - My Site {% endblock %}</title>
<link rel="stylesheet" href="">
</head>
<body>

   {% include 'header.html' %}


   {% block content %}
     <div> this is the layouts <div>
   {% endblock %}


  </body>
</html>

index.html:

 {% extends 'layout.html' %}

  {% block content %}
   <div>this is index page</div>
{% endblock %}

【问题讨论】:

    标签: flask


    【解决方案1】:

    Jinja 的概念有点不同。如果您在 index.html 文件中扩展 layout.html ,您也必然需要在其中调用 header 替换。所以标题没有被调用。这是一个例子:

    {% extends 'layout.html' %}
    
    {% block header %}
    <div>this is header</div>
    {% endblock %}
    
    {% block content %}
    <div>this is index page</div>
    {% endblock %}
    

    我开发了一个项目,可以帮助您进行一系列自动创作,如果您想冒险并帮助我改进它,请放心:https://github.com/marcosstefani/flute

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-17
      • 2016-02-26
      • 1970-01-01
      • 1970-01-01
      • 2013-12-31
      • 1970-01-01
      • 2016-04-03
      • 1970-01-01
      相关资源
      最近更新 更多