Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,130 --> 00:00:04,430
So now with our models all set up
2
00:00:04,430 --> 00:00:08,250
and with some data being added for our admin view,
3
00:00:08,250 --> 00:00:12,690
I now wanna make sure that for the front-end of our blog.
4
00:00:12,690 --> 00:00:15,690
So what our users, our visitors see,
5
00:00:15,690 --> 00:00:19,180
we of course also now use that data from the database
6
00:00:19,180 --> 00:00:20,960
with help of the models.
7
00:00:20,960 --> 00:00:22,960
And currently in the views py
8
00:00:22,960 --> 00:00:25,330
we are of course using that dummy data
9
00:00:25,330 --> 00:00:29,600
that all posts list with our dummy dictionaries.
10
00:00:29,600 --> 00:00:32,870
I will now remove that here
11
00:00:32,870 --> 00:00:36,630
or I will at least remove those dummy posts in there
12
00:00:36,630 --> 00:00:39,730
so that we can rule out that data's coming from there.
13
00:00:39,730 --> 00:00:40,810
Because now the goal
14
00:00:40,810 --> 00:00:44,420
is to step-by-step migrated those view functions
15
00:00:44,420 --> 00:00:47,890
to use the models and get the data from there.
16
00:00:47,890 --> 00:00:50,610
And let's start with the function
17
00:00:50,610 --> 00:00:53,220
which is responsible for this starting page
18
00:00:53,220 --> 00:00:55,960
where we also of course have these free block posts.
19
00:00:55,960 --> 00:00:59,040
So this starting page function here.
20
00:00:59,040 --> 00:01:03,930
There are in the past, we basically sorted all posts by date
21
00:01:03,930 --> 00:01:06,080
with that get date help or function,
22
00:01:06,080 --> 00:01:10,220
and then we got the last free posts.
23
00:01:10,220 --> 00:01:13,690
Now, of course, the goal is now to use our model,
24
00:01:13,690 --> 00:01:17,370
and therefor the first step is to import
25
00:01:17,370 --> 00:01:21,860
from the models file here in that directory,
26
00:01:21,860 --> 00:01:24,543
import the post model.
27
00:01:27,000 --> 00:01:29,630
And then in starting page, we can build a query
28
00:01:29,630 --> 00:01:33,210
as we learn it in the Shell over the last sections.
29
00:01:33,210 --> 00:01:37,390
We can reach out to our posts with post objects
30
00:01:37,390 --> 00:01:41,893
and we could get all posts with that all method here.
31
00:01:43,100 --> 00:01:46,270
We could also filter for a post if we wanted to,
32
00:01:46,270 --> 00:01:48,920
but here we don't really want to filter.
33
00:01:48,920 --> 00:01:50,830
We only have two restrictions,
34
00:01:50,830 --> 00:01:54,040
and that's a limit of posts which you wanna fetch,
35
00:01:54,040 --> 00:01:55,990
we only wanna get three posts,
36
00:01:55,990 --> 00:01:58,253
and we wanna order our posts.
37
00:01:59,100 --> 00:02:02,790
Now for ordering, we can call order by,
38
00:02:02,790 --> 00:02:05,910
we can call this on the result of filter,
39
00:02:05,910 --> 00:02:08,570
but we can also call it on the result of all
40
00:02:08,570 --> 00:02:11,670
since we can generally call it on any query set
41
00:02:11,670 --> 00:02:15,800
and all the just returns such a query set.
42
00:02:15,800 --> 00:02:18,000
So here we can also call order by
43
00:02:18,000 --> 00:02:20,780
and then pass in the key by which you wanna order.
44
00:02:20,780 --> 00:02:24,000
And that would be the date here.
45
00:02:24,000 --> 00:02:26,040
So one of the fields in our model,
46
00:02:26,040 --> 00:02:30,080
in this case, in the post model, that date field.
47
00:02:30,080 --> 00:02:33,780
I wanna filter by that and we can filter an ascending order
48
00:02:33,780 --> 00:02:35,620
or in descending order.
49
00:02:35,620 --> 00:02:39,100
And I wanna filter in descending order with a minus here
50
00:02:39,100 --> 00:02:44,100
to make sure that's the post we added the last is on top.
51
00:02:44,420 --> 00:02:46,780
Now that gives us the ordered posts,
52
00:02:46,780 --> 00:02:49,020
but we only wanna get three.
53
00:02:49,020 --> 00:02:52,330
And here's something very neat which we can do with Django,
54
00:02:52,330 --> 00:02:54,900
which we haven't seen up to this point in this course,
55
00:02:54,900 --> 00:02:57,300
which you might have done in your solution still,
56
00:02:57,300 --> 00:02:59,590
and which is absolutely correct.
57
00:02:59,590 --> 00:03:03,650
We cannot also take a slice of that result here
58
00:03:03,650 --> 00:03:07,590
with that default slicing syntax we know from Python,
59
00:03:07,590 --> 00:03:09,260
so by adding square brackets,
60
00:03:09,260 --> 00:03:13,180
and then to take only the first free elements,
61
00:03:13,180 --> 00:03:15,580
we use this slicing syntax.
62
00:03:15,580 --> 00:03:18,500
Now a couple of important annotations here though
63
00:03:18,500 --> 00:03:19,860
from my site.
64
00:03:19,860 --> 00:03:23,500
For one, you might think that this is bad for performance,
65
00:03:23,500 --> 00:03:27,040
because you could think that here Django reaches out
66
00:03:27,040 --> 00:03:30,000
to the database, gets all posts
67
00:03:30,000 --> 00:03:33,510
and then slices all posts in Python.
68
00:03:33,510 --> 00:03:35,490
And that would be very inefficient
69
00:03:35,490 --> 00:03:38,080
because we would needlessly fetch all posts
70
00:03:38,080 --> 00:03:41,110
from the database, even though we only need three,
71
00:03:41,110 --> 00:03:43,550
and then we would filter them in Python.
72
00:03:43,550 --> 00:03:46,250
So we would get way too many results from the database
73
00:03:46,250 --> 00:03:49,670
and then also have a performance hit when we filter
74
00:03:49,670 --> 00:03:52,920
or when we slice these results in Python.
75
00:03:52,920 --> 00:03:55,790
But thankfully this is not what's going on.
76
00:03:55,790 --> 00:03:57,440
Django is smart
77
00:03:57,440 --> 00:04:01,210
and it will actually convert this entire statement here
78
00:04:01,210 --> 00:04:03,800
into a SQL command.
79
00:04:03,800 --> 00:04:07,070
So it will not get all ordered posts,
80
00:04:07,070 --> 00:04:10,910
but it takes this slicing syntax also into account
81
00:04:10,910 --> 00:04:14,520
and actually creates one long query, one long SQL query
82
00:04:14,520 --> 00:04:18,450
based on this entire line where it already slices
83
00:04:18,450 --> 00:04:21,140
when fetching the data from the database.
84
00:04:21,140 --> 00:04:24,690
So it only fetches free results from the database
85
00:04:24,690 --> 00:04:26,070
because of this line.
86
00:04:26,070 --> 00:04:29,050
That's some optimization which Django does for us
87
00:04:29,050 --> 00:04:31,590
behind the scenes automatically.
88
00:04:31,590 --> 00:04:34,610
It is important to know though that for this,
89
00:04:34,610 --> 00:04:37,860
Django does not support negative indexing here.
90
00:04:37,860 --> 00:04:41,060
So -3: as we did it before
91
00:04:41,060 --> 00:04:43,440
would not be supported here.
92
00:04:43,440 --> 00:04:46,860
But that's no problem because since I'm sorting by date
93
00:04:46,860 --> 00:04:49,020
in a descending order already,
94
00:04:49,020 --> 00:04:51,740
I now wanna get the first free dates
95
00:04:51,740 --> 00:04:55,170
because since we're starting with the most recent date,
96
00:04:55,170 --> 00:04:59,260
because of the ordering, I wanna take the first three posts
97
00:04:59,260 --> 00:05:01,763
because that will be the three most recent posts.
98
00:05:02,750 --> 00:05:07,360
So therefore this one line does all they need to do,
99
00:05:07,360 --> 00:05:09,570
it gets us the free latest posts
100
00:05:09,570 --> 00:05:12,830
and therefor I can now get latest posts
101
00:05:14,190 --> 00:05:17,293
and pass them on to posts here to my starting page.
102
00:05:18,890 --> 00:05:20,540
Now, if we save that
103
00:05:21,460 --> 00:05:24,330
and we go back and reload that starting page,
104
00:05:24,330 --> 00:05:28,420
I get an error, failed to look up for key image
105
00:05:28,420 --> 00:05:29,783
in post object.
106
00:05:30,800 --> 00:05:34,870
And that makes sense because in my post model here,
107
00:05:34,870 --> 00:05:37,920
I do have a key named image name,
108
00:05:37,920 --> 00:05:40,830
that's where my image name is stored in.
109
00:05:40,830 --> 00:05:45,200
But in my template, if I have a look at the index html file
110
00:05:45,200 --> 00:05:48,200
for the starting page, there are in this template
111
00:05:49,440 --> 00:05:52,840
in the post then, in that included post,
112
00:05:52,840 --> 00:05:56,100
I am referring to just .image.
113
00:05:56,100 --> 00:05:57,450
This should now .image_name
114
00:05:59,020 --> 00:06:02,330
since that's the field name in the post model.
115
00:06:02,330 --> 00:06:06,230
And we are now working with that post model.
116
00:06:06,230 --> 00:06:08,593
So that's something we need to change and save.
117
00:06:10,310 --> 00:06:12,280
And then we can reload here.
118
00:06:12,280 --> 00:06:14,570
And this now looks way better.
119
00:06:14,570 --> 00:06:17,660
Now those posts are being loaded correctly
120
00:06:18,550 --> 00:06:22,060
and therefore let's now move on to all posts
121
00:06:22,060 --> 00:06:24,510
where of course now currently nothing is showing up
122
00:06:24,510 --> 00:06:27,510
because we haven't updated this method yet.
123
00:06:27,510 --> 00:06:30,280
In case you haven't implemented it on your own yet,
124
00:06:30,280 --> 00:06:33,360
maybe try implementing this page on your own now.
125
00:06:33,360 --> 00:06:36,210
In the next lecture, we're going to do it together again.
10108
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.