Course Content
Flask Intensive Course: Web Development with Python
Flask Intensive Course: Web Development with Python
Imitating Database
Let's imitate data as data from the Database, which we will implement later on.
We should pass our list (or database) as an argument, treating it like a table where the keys represent column titles, and each dictionary corresponds to the entries of a row.
The second dictionary does not have an author for a purpose.
To proceed, let's create a new file in the templates directory and name it recipes.html. We'll use this filename as an argument in our function.
Just repeat the code with blocks as in index.html pages.
As we dive into working with several recipes, we'll be passing a list of recipes as an argument. To handle this in Jinja2, we'll harness the power of loops. There are times when we won't know in advance how many items our table will contain, making manual writing impractical. This is where the {% %}
syntax for for loops comes to our rescue.
To extract values from the table or dictionary, as we'll do in our scenario, we'll use the double curly brackets, like so: {{ }}
.
Furthermore, we can employ the if statement within the same curly brackets and percent sign syntax {% %}
. In our case, we'll check whether author information is available in the table, and based on that, we'll either display the author's name or an "N/A" message.
index
index
index
The result is:
Thanks for your feedback!