Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Flask Basics Templates and Static Files Template Inheritance

Melih Mucuk
Melih Mucuk
2,118 Points

Didn't get the right `<title>` in the rendered template.

Task:

Finally, change the "index.html" <title> tag to be: {{ super() }} Homepage. Make sure there's a space before "Homepage".

Error:

"Didn't get the right < title > in the rendered template."

What was the problem ?

flask_app.py
from flask import Flask
from flask import render_template

app = Flask(__name__)


@app.route('/')
def index():
    return render_template('index.html')
templates/index.html
{% extends "layout.html" %}

{% block title %}{{ super() }} Homepage{% endblock %}

{% block content%}
Smells Like Bakin'!
Welcome to my bakery web site!
{% endblock %}
templates/layout.html
<!doctype html>
<html>
<head><title>{% block title %}{% endblock %} Smells Like Bakin'</title></head>
<body>
  {% block content %}{% endblock %}
</body>
</html>

Wowww how did you include the filename in your code block??

4 Answers

Dan Johnson
Dan Johnson
40,532 Points

In the layout template, you'll want "Smells Like Bakin'" inside the title block, like this:

<head><title>{% block title %}Smells Like Bakin'{% endblock %}</title></head>

You'll also want to maintain the HTML tags in the content block in the index template:

{% block content %}
<h1>Smells Like Bakin'!</h1>
<p>Welcome to my bakery web site!</p>
{% endblock %}

Earlier in one of the tasks entering the title as 'Smells Like Bakin' did not work well. Entering Smells Like Bakin' works.

Whereas in this task there is no apostrophe in {% block title %}{{ super() }} Homepage{% endblock %} .

Had to remove the apostrophe to pass this task, why so?

task 1

Add two blocks to the layout.html template Add a block named title around the content of the title tag add a block named content inside the body tag.

from flask import Flask

from flask import render_template

app = Flask(name)

@app.route('/')

def index():

return render_template('index.html')


templates/index.html

<!doctype html>

<html>

<head><title>{% block title %} Smells Like Bakin' {% endblock %}</title></head>

<body>

{% block content %}

<p>welcome to my bakery web site!</p>

{% endblock %}

</body>

</html>


templates/layout.html

<!doctype html>

<html>

<head><title>{% block title %}Smells Like Bakin'{% endblock %}</title></head>

<head><title>Smells Like Bakin'</title></head>

<body>

{% block content %}{% endblock %}

</body>

</html>


task 2

templates/index.html <!doctype html> <html> <head><title>{% block title %} Smells Like Bakin' {% endblock %}</title></head> <body> {% block content %} <p>welcome to my bakery web site!</p> {% endblock %} {%extends "layout.html"%} {%block title%}Homepage{%endblock%} {%block content%}Smells Like Bakin'!Welcome to my bakery web site!{%endblock%} </body> </html>

task 3

templates/index.html

<!doctype html> <html> <head><title>{% block title %} Smells Like Bakin' {% endblock %}</title></head> <body> {% block content %} <p>welcome to my bakery web site!</p> {% endblock %} {%extends "layout.html"%} {%block title%}Homepage{%endblock%} {%block content%}Smells Like Bakin'!Welcome to my bakery web site!{%endblock%} </body> </html>

task 4

template/index.html

Put the contents of the <title> tag in "index.html" into the title block.

{%extends "layout.html"%} <!doctype html> <html> <head><title>{%block title%}Homepage{%endblock%}</title></head> <body> {%block content%}Smells Like Bakin'!Welcome to my bakery web site!{%endblock%} </body>

</html>

task 5

Remove everything from "index.html" except for the extends

and block tags and their contents.

template/index.html

{%extends "layout.html"%} {%block title%}Homepage{%endblock%} {%block content%}<h1>Smells Like Bakin'!</h1><p>Welcome to my bakery web site!</p>{%endblock%}

template/layout.html <!doctype html> <html> <head><title>{%block title%}Smells Like Bakin'{%endblock%}</title></head> <body> {%block content%}{%endblock%} </body>

</html>

task 6

{% block title %}{{ super() }} Homepage{% endblock %}

====================

<!-- layout.html --> <!doctype html> <html> <head><title>{% block title %}Smells Like Bakin'</title>{% endblock %}</head> <body> {% block content %}{% endblock %}

</body> </html>

<!-- index.html--> {% extends "layout.html" %} {% block title %} {{ super() }} | Homepage {% endblock %} {% block content %} <h1>Smells Like Bakin'!</h1> <p>Welcome to my bakery web site!</p> {% endblock %}

Task 4

{% extends "layout.html" %}
<!doctype html>
<html>
<head><title>{% block title %}Homepage {%endblock%}</title></head>
<body>
  {% block content %}
<h1>Smells Like Bakin'!</h1>
<p>Welcome to my bakery web site!</p>
  {% endblock %}
</body>
</html>