Okay.
In this video, I will show you how we can pass a dynamic data to our template and we can use it here.
So first thing, what we will need to do is I will close all of these files here and I will focus on
on the views.
So what we have here is we have a simple render, but render also accepts a third argument, which is
an object.
So basically what we can do is we can pass object to our template and we can use whatever will be passed
in this object in our template.
So let's do a quick test.
I will pass, let's say a variable called data, and then we can pass a simple string.
This is a data from views.
So what we are passing is passing object.
And in that object we have data and data is that string.
So if I go back to the first step here, what I can do is in this H2, I can do curly brackets like
this opening and closing double curly brackets.
And inside here I will do data.
Data is the name that I passed here, so you can name it whatever you like and you need to reference
that here.
So if I will save it here and come back to our view.
And then that was the previous message we had.
And then if I refresh it, this is data from views.
So basically what we've done is now we have dynamic data here and we passing that data from our views.
So whatever is available in the views might be available also in HTML.
So let's give it a try and let's create a variable books and then we can do book as we had it before,
open objects and then we can do all.
So basically we will select all books from our database.
And then what I can do is I can name it books and also I can pass instead of a string, I can pass books.
So what's happening now is I select all books from the database and I pass it to the template with name
books.
Hey, come back to the template.
We don't have a date anymore.
Let's see what's available in the books.
You can see that it's not going to be readable, but it will be available in our template.
So this is a query set and you can see here book object one, book, object two.
So at the moment that's not looking nice, but at least we have an access to our books and we can do
something about it.
So let's come here and let's change the way we we display this at the moment.
This is array query set of the old books, but we, we can have an access to our to this array so we
can do something about it.
And then this Django use a Jinja templating system.
So we have a lot of options here.
I'm not going to go through all of them, but I will show you a few examples what we can do here.
And then if you ever need to do something like that, you will know how to do it.
So basically what we do is we open curly brackets and we close it.
And then inside this we need to also have percentage sign.
That's a starting point.
And then we can do for book in books.
That's a typical.
Python expression for a loop.
And then what we need to also have is.
Ending of that expression.
So we do end for like that.
So we have that kind of syntax opening and closing.
And then we have expression here for book in books and then we have end for whatever is inside here
we have an access.
Here to our book.
So.
So instead of displaying the book as an object, I will go book title.
So let's save it now and see how it looks like.
I'll refresh this.
And something is wrong.
Uh, let's come back here.
And this looks okay.
Let's come back here.
We have books past here and then.
Hello.
Refresh this.
And this is still not available.
So let me try to debug it.
I will put here
books and see if that's still available on our template.
It is not for some reason.
You don't see any errors?
And this needs to be books, not a book.
Refresh it again.
Okay, so we have our query set.
That's fine.
And then for some reason, this is not triggering.
Okay.
I know where I made a mistake.
And I have a book's title here.
In fact, we are doing a loop here.
So each time we do a loop, it's a book that we want to refer to, not the books array.
So I will remove this and the book title and then I can remove that.
I will come back to our template and then refresh it.
You can see Hobbit and Lord of the Ring, and this is coming from our database.
So we dynamically select things from our database and then we display that in our template or pass it
to the template and then we can do whatever we like with this.
So for example, that's a syntax for a for a for we might have another one for if.
So if I will do here.
And then I said if.
Book is.
Published.
Then we will display this and then we need to have another end.
So whatever we start something, we need to also end it.
So I will go here and then end.
This time we will end if.
So we have another block here.
If book is published that is published is a part of our model, then print this.
Otherwise don't.
So I can save it now and I can come back here and refresh it and we can see only Lords of the Ring is
displayed here because we marked Lord of the Ring as is published.
The order book is not so as you can see here, we have a lot of different options.
So if you know HTML, CSS and some JavaScript, you can do whatever you like.
You will have dynamic data from Django, from the database served to you.
You will pass it with with an object and you can name it as you like and you can take and pass whatever
you like actually from here.
And then you can display that in the format you like.
So that's how powerful Django is.
It's not like just a back end framework.
It also helps you to serve dynamic websites if you need it.
But as I said before in our tutorial, we will be focusing on having a back end for storing our data
and serving using the Json format.
So we will ask for the data, we will have it in a Json format and then we will use it with some other
front end frameworks.