Chebevara
Chebevara

Reputation: 194

Child template isn't rendering

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

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

<!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

{% 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 %}

On http://localhost:5000/ I get:

<!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>

Upvotes: 2

Views: 1413

Answers (1)

davidism
davidism

Reputation: 127190

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')

Upvotes: 6

Related Questions