Before we resolve our “TemplateNotFound” error, let’s go more into depth on templates. Up until now, we’ve included the templates your application needed to work, but this time we’ll create a new one.
Thinking back to the Building Blocks of the Web workshop, the first step here is to build a page using HTML. One limitation of using plain HTML is that it won’t allow us to embed data from our application code in it easily, but luckily Flask gives us templates that make it easier to do this. Templates allow us to print the values of Python variables in our HTML files, and allow us to run some Python code in them such as loops. This is how our applications can become more dynamic.
Templates also allow us to reuse small snippets of HTML rather than creating full HTML files for each part of our application. For example, if you had the same header on every page, you could write the HTML for the header once, and then include it in other pages. That way you’ll only need to change it one place, rather than on every page of your application. You’ll have noticed that in our previous templates, there were these lines:
{% extends 'base.html' %}
{% block content %}
...HTML content...
{% endblock %}
This is an example of template reuse. base.html contains the base layout for the application, which is applied to every page in our application. Go try to add an h1 HTML tag in your base.html template like this:
<main class="container">
<h1>My Awesome Application</h1>
{% block content %}{% endblock %}
</main>
Then look at any page of your web application. What happened?
Let’s resolve that “TemplateNotFound” error, by adding a new template at templates/books.html:
{% extends 'base.html' %}
{% block content %}
<a href="{{ url_for('new_book') }}" class="btn btn-lg btn-primary">Create New Book</a>
<h1>My Book List</h1>
{% for book in books %}
<h2>{{ book.id }}</h2>
<p><a href="{{ url_for('show_book', book_id=book.id) }}">{{ book.title }}</a></p>
<small>submitted on {{ book.created_at }}</small>
{% endfor %}
{% endblock %}
Now visit http://localhost:3000/books
How many books do you see? Let us know in chat.
Note how we pass in the variable all_books to our template, and give it the value books. If you look at the template you’ll see that the template includes some markup like {{ book.title }} which is used to substitute in the value of variables.
Here you’ll see some of the power of templates. We want to display the data from each book in the database on the page. Templates allow us to run certain Python code from within our HTML.
In the above template, we run a for loop that iterates through the list of books. The only difference between this template code and standard Python code is that we need to tell the template we are including code, and we do that by enclosing it in {% %}, and we also need to tell the template that we are ending the for loop with the {% endfor %} line, since it can’t rely on indentation in the same way that Python code can.
We can then display each field associated with the book database table (id, title, description, cover, reason, created_at) using dot notation (eg, book.id, book.title, book.created_at).
Different frameworks may use a different syntax for how to replace variables in templates.