子模板未呈现 [英] Child template isn't rendering

查看:45
本文介绍了子模板未呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个布局模板和一个子模板.但是,子模板中没有显示其他信息.为什么不使用子模板中的信息?

I have a layout template and a child template. However, none of the extra information from the child template is showing up. Why isn't the information in the child template being used?

FlaskTest.py

FlaskTest.py

from flask import Flask, render_template

app = Flask(__name__)


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


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

layout.html

layout.html

<!doctype html>
<html>
  <head>
    {% block head %}
    <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
    <title>{% block title %}{% endblock %} - My Webpage</title>
    {% endblock %}
  </head>
  <body>
    <div id="content">{% block content %}{% endblock %}</div>
    <div id="footer">
      {% block footer %}
      &copy; Copyright 2010 by <a href="http://domain.invalid/">you</a>.
      {% endblock %}
    </div>
  </body>
</html>

child.html

child.html

{% extends 'layout.html' %}
{% block title %}Index{% endblock %}

{% block head %}
    {{ super() }}
    <style type="text/css">
        .important {
            color: #336699;
        }
    </style>
{% endblock %}

{% block content %}
    <h1>Index</h1>
    <p class="important">
        Welcome on my awesome homepage.
{% endblock %}

http://localhost:5000/上,我得到了:

<!doctype html>
<html>
  <head>

    <link rel="stylesheet" href="/static/style.css">
    <title> - My Webpage</title>

  </head>
  <body>
    <div id="content"></div>
    <div id="footer">

      &copy; Copyright 2010 by <a href="http://domain.invalid/">you</a>.

    </div>
  </body>
</html>

推荐答案

您需要呈现child.html,而不是layout.html.子级会扩展布局,但是您编写它的方式只会渲染布局.

You need to render child.html, not layout.html. Child will extend layout, but the way you've written it, only layout will be rendered.

return render_template('child.html')

这篇关于子模板未呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆