WEBVTT

00:01.170 --> 00:01.680
Okay.

00:01.680 --> 00:08.100
In this video, I will show you how we can pass a dynamic data to our template and we can use it here.

00:08.100 --> 00:16.080
So first thing, what we will need to do is I will close all of these files here and I will focus on

00:16.080 --> 00:17.220
on the views.

00:17.220 --> 00:26.340
So what we have here is we have a simple render, but render also accepts a third argument, which is

00:26.340 --> 00:27.300
an object.

00:27.300 --> 00:33.840
So basically what we can do is we can pass object to our template and we can use whatever will be passed

00:33.840 --> 00:35.970
in this object in our template.

00:35.970 --> 00:37.620
So let's do a quick test.

00:37.740 --> 00:46.920
I will pass, let's say a variable called data, and then we can pass a simple string.

00:47.130 --> 00:51.600
This is a data from views.

00:53.460 --> 00:56.430
So what we are passing is passing object.

00:56.430 --> 01:01.240
And in that object we have data and data is that string.

01:01.240 --> 01:13.750
So if I go back to the first step here, what I can do is in this H2, I can do curly brackets like

01:13.750 --> 01:16.930
this opening and closing double curly brackets.

01:16.930 --> 01:19.540
And inside here I will do data.

01:19.570 --> 01:26.290
Data is the name that I passed here, so you can name it whatever you like and you need to reference

01:26.290 --> 01:26.860
that here.

01:26.860 --> 01:30.460
So if I will save it here and come back to our view.

01:30.460 --> 01:33.520
And then that was the previous message we had.

01:33.520 --> 01:36.730
And then if I refresh it, this is data from views.

01:36.730 --> 01:46.150
So basically what we've done is now we have dynamic data here and we passing that data from our views.

01:46.150 --> 01:51.340
So whatever is available in the views might be available also in HTML.

01:51.460 --> 02:01.750
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,

02:02.000 --> 02:06.820
open objects and then we can do all.

02:07.180 --> 02:11.110
So basically we will select all books from our database.

02:11.110 --> 02:18.910
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.

02:19.000 --> 02:25.720
So what's happening now is I select all books from the database and I pass it to the template with name

02:25.720 --> 02:26.470
books.

02:29.280 --> 02:30.600
Hey, come back to the template.

02:30.630 --> 02:32.070
We don't have a date anymore.

02:32.100 --> 02:34.380
Let's see what's available in the books.

02:34.410 --> 02:39.660
You can see that it's not going to be readable, but it will be available in our template.

02:39.660 --> 02:45.210
So this is a query set and you can see here book object one, book, object two.

02:45.210 --> 02:51.060
So at the moment that's not looking nice, but at least we have an access to our books and we can do

02:51.060 --> 02:51.900
something about it.

02:51.900 --> 02:58.500
So let's come here and let's change the way we we display this at the moment.

02:58.500 --> 03:07.650
This is array query set of the old books, but we, we can have an access to our to this array so we

03:07.650 --> 03:09.270
can do something about it.

03:09.270 --> 03:13.770
And then this Django use a Jinja templating system.

03:13.770 --> 03:15.720
So we have a lot of options here.

03:15.720 --> 03:20.940
I'm not going to go through all of them, but I will show you a few examples what we can do here.

03:20.940 --> 03:25.500
And then if you ever need to do something like that, you will know how to do it.

03:25.830 --> 03:31.210
So basically what we do is we open curly brackets and we close it.

03:31.330 --> 03:36.010
And then inside this we need to also have percentage sign.

03:36.400 --> 03:38.650
That's a starting point.

03:38.650 --> 03:44.560
And then we can do for book in books.

03:45.580 --> 03:47.050
That's a typical.

03:47.820 --> 03:49.830
Python expression for a loop.

03:49.830 --> 03:53.970
And then what we need to also have is.

03:55.670 --> 03:57.380
Ending of that expression.

03:57.380 --> 04:01.820
So we do end for like that.

04:02.120 --> 04:07.040
So we have that kind of syntax opening and closing.

04:07.040 --> 04:15.740
And then we have expression here for book in books and then we have end for whatever is inside here

04:16.550 --> 04:18.080
we have an access.

04:20.700 --> 04:22.440
Here to our book.

04:22.560 --> 04:23.280
So.

04:23.520 --> 04:29.310
So instead of displaying the book as an object, I will go book title.

04:29.730 --> 04:32.880
So let's save it now and see how it looks like.

04:34.200 --> 04:35.490
I'll refresh this.

04:37.300 --> 04:39.940
And something is wrong.

04:42.200 --> 04:44.710
Uh, let's come back here.

04:46.590 --> 04:48.440
And this looks okay.

04:48.450 --> 04:49.500
Let's come back here.

04:49.500 --> 04:53.580
We have books past here and then.

05:00.370 --> 05:00.610
Hello.

05:00.610 --> 05:01.810
Refresh this.

05:06.670 --> 05:08.500
And this is still not available.

05:09.550 --> 05:11.900
So let me try to debug it.

05:11.920 --> 05:15.130
I will put here

05:17.320 --> 05:23.470
books and see if that's still available on our template.

05:25.630 --> 05:27.640
It is not for some reason.

05:29.670 --> 05:31.190
You don't see any errors?

05:34.010 --> 05:36.650
And this needs to be books, not a book.

05:39.020 --> 05:40.130
Refresh it again.

05:40.280 --> 05:41.990
Okay, so we have our query set.

05:42.020 --> 05:42.950
That's fine.

05:43.940 --> 05:47.510
And then for some reason, this is not triggering.

05:50.200 --> 05:50.470
Okay.

05:50.470 --> 05:52.180
I know where I made a mistake.

05:52.180 --> 05:54.370
And I have a book's title here.

05:54.370 --> 05:56.770
In fact, we are doing a loop here.

05:56.770 --> 06:02.850
So each time we do a loop, it's a book that we want to refer to, not the books array.

06:02.890 --> 06:09.430
So I will remove this and the book title and then I can remove that.

06:11.010 --> 06:13.890
I will come back to our template and then refresh it.

06:13.920 --> 06:18.900
You can see Hobbit and Lord of the Ring, and this is coming from our database.

06:18.900 --> 06:25.920
So we dynamically select things from our database and then we display that in our template or pass it

06:25.920 --> 06:29.280
to the template and then we can do whatever we like with this.

06:29.280 --> 06:36.810
So for example, that's a syntax for a for a for we might have another one for if.

06:36.840 --> 06:40.710
So if I will do here.

06:40.920 --> 06:43.050
And then I said if.

06:45.530 --> 06:47.510
Book is.

06:48.920 --> 06:49.850
Published.

06:51.860 --> 06:56.480
Then we will display this and then we need to have another end.

06:56.570 --> 07:00.350
So whatever we start something, we need to also end it.

07:00.350 --> 07:03.260
So I will go here and then end.

07:03.290 --> 07:05.060
This time we will end if.

07:05.450 --> 07:08.090
So we have another block here.

07:08.090 --> 07:14.750
If book is published that is published is a part of our model, then print this.

07:14.750 --> 07:16.270
Otherwise don't.

07:16.280 --> 07:23.600
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

07:23.600 --> 07:27.540
displayed here because we marked Lord of the Ring as is published.

07:27.540 --> 07:33.890
The order book is not so as you can see here, we have a lot of different options.

07:33.890 --> 07:39.680
So if you know HTML, CSS and some JavaScript, you can do whatever you like.

07:39.710 --> 07:44.540
You will have dynamic data from Django, from the database served to you.

07:44.570 --> 07:51.620
You will pass it with with an object and you can name it as you like and you can take and pass whatever

07:51.720 --> 07:53.460
you like actually from here.

07:53.460 --> 07:57.660
And then you can display that in the format you like.

07:57.660 --> 08:00.750
So that's how powerful Django is.

08:00.780 --> 08:03.330
It's not like just a back end framework.

08:03.330 --> 08:09.030
It also helps you to serve dynamic websites if you need it.

08:09.030 --> 08:16.260
But as I said before in our tutorial, we will be focusing on having a back end for storing our data

08:16.260 --> 08:18.930
and serving using the Json format.

08:18.930 --> 08:26.760
So we will ask for the data, we will have it in a Json format and then we will use it with some other

08:26.760 --> 08:28.290
front end frameworks.
