Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,160 --> 00:00:05,530
So let's now work on that all posts page
2
00:00:05,530 --> 00:00:09,063
in the views.py file that is that posts function here.
3
00:00:10,060 --> 00:00:11,940
Now, there, we are doing something
4
00:00:11,940 --> 00:00:13,290
very simple right now.
5
00:00:13,290 --> 00:00:16,090
We're passing this all posts list,
6
00:00:16,090 --> 00:00:19,860
to the all posts variable in the template.
7
00:00:19,860 --> 00:00:22,630
Well, this all posts list is now an empty list,
8
00:00:22,630 --> 00:00:25,170
and therefore this won't work anymore.
9
00:00:25,170 --> 00:00:27,480
And stepping out I wanna get all the posts
10
00:00:27,480 --> 00:00:29,590
from the database.
11
00:00:29,590 --> 00:00:31,940
And of course that's fairly straightforward.
12
00:00:31,940 --> 00:00:35,900
We can add all posts here and keep that name if we want to,
13
00:00:35,900 --> 00:00:38,683
and then reach out to post, to the post module,
14
00:00:39,540 --> 00:00:41,620
objects.all.
15
00:00:41,620 --> 00:00:44,340
And this gives us all the posts.
16
00:00:44,340 --> 00:00:47,860
Now we can also sort those in any way if we want to,
17
00:00:47,860 --> 00:00:49,610
and actually I'll do that here.
18
00:00:49,610 --> 00:00:53,210
I'll also order those by date
19
00:00:53,210 --> 00:00:57,270
in a descending order to put the latest post first,
20
00:00:57,270 --> 00:00:59,140
but that's of course optional.
21
00:00:59,140 --> 00:01:01,000
But with that, we're now having a list
22
00:01:01,000 --> 00:01:03,250
of all the posts from the database
23
00:01:03,250 --> 00:01:06,413
and we're passing this to all posts in the template.
24
00:01:07,270 --> 00:01:11,690
So now, if we save this and we reload this post page,
25
00:01:11,690 --> 00:01:14,743
we see all free posts here.
26
00:01:16,170 --> 00:01:17,690
Now for a single post,
27
00:01:17,690 --> 00:01:20,750
if I click on that single post we get an error.
28
00:01:20,750 --> 00:01:21,920
And that makes a lot of sense
29
00:01:21,920 --> 00:01:26,040
because we're clearly failing to fetch that single post now.
30
00:01:26,040 --> 00:01:29,370
So now we wanna work on post detail.
31
00:01:29,370 --> 00:01:33,930
And there before, we basically worked with all posts
32
00:01:33,930 --> 00:01:37,263
to find that one post with the correct slug.
33
00:01:38,180 --> 00:01:41,500
Now, of course, that's not what we wanna do anymore.
34
00:01:41,500 --> 00:01:44,730
I wanna get rid of that line of code here,
35
00:01:44,730 --> 00:01:48,170
and we can also get rid of all posts all together here,
36
00:01:48,170 --> 00:01:50,130
since we're not using that anymore.
37
00:01:50,130 --> 00:01:52,100
We can get rid of that date, time imports
38
00:01:52,100 --> 00:01:54,500
since we're not using the date function here anymore.
39
00:01:54,500 --> 00:01:57,170
And we can get rid of that (indistinct) get date function
40
00:01:57,170 --> 00:02:00,560
as well, to slim this file down.
41
00:02:00,560 --> 00:02:02,700
And now back in post detail,
42
00:02:02,700 --> 00:02:06,780
I still wanna get my identified post.
43
00:02:06,780 --> 00:02:08,759
So this name, which I used here,
44
00:02:08,759 --> 00:02:10,590
and now I wanna get a single post.
45
00:02:10,590 --> 00:02:12,440
And you'll learn how that works.
46
00:02:12,440 --> 00:02:15,940
We can use the post model, and then objects,
47
00:02:15,940 --> 00:02:18,950
and then get, instead of all.
48
00:02:18,950 --> 00:02:21,970
And this gets us a single post.
49
00:02:21,970 --> 00:02:24,890
And here, we then can tell Django
50
00:02:24,890 --> 00:02:27,640
how this post should be identified.
51
00:02:27,640 --> 00:02:29,910
In this case that slug,
52
00:02:29,910 --> 00:02:34,313
so that slug field in our post model,
53
00:02:35,230 --> 00:02:38,200
the value there should be equal to the slug value
54
00:02:38,200 --> 00:02:40,080
we're receiving here.
55
00:02:40,080 --> 00:02:42,730
So slug on the right side is this argument,
56
00:02:42,730 --> 00:02:46,120
slug on the left side is the name of that property,
57
00:02:46,120 --> 00:02:48,023
of that attribute in our model.
58
00:02:49,980 --> 00:02:51,730
Now, of course, this could fail
59
00:02:51,730 --> 00:02:55,380
and therefore we could wrap this in a try except blog
60
00:02:55,380 --> 00:02:58,620
to show a 404 page, if it does fail.
61
00:02:58,620 --> 00:03:01,600
But I did show you a little shortcut,
62
00:03:01,600 --> 00:03:03,910
which you can use instead,
63
00:03:03,910 --> 00:03:07,870
where we import from Django shortcuts, not just render,
64
00:03:07,870 --> 00:03:10,793
but also get object or 404.
65
00:03:12,170 --> 00:03:16,140
And with that imported, we can get our post here,
66
00:03:16,140 --> 00:03:19,860
simply by instead calling get, whoops!
67
00:03:19,860 --> 00:03:23,390
Get object or 404,
68
00:03:23,390 --> 00:03:26,670
pass in our post model here
69
00:03:26,670 --> 00:03:29,490
to let Django know for which model it should find
70
00:03:29,490 --> 00:03:31,170
an entry in the database.
71
00:03:31,170 --> 00:03:34,063
And then our condition, which is slug equals slug.
72
00:03:35,400 --> 00:03:37,450
With that we get the identified posts.
73
00:03:37,450 --> 00:03:39,260
And if we now save this,
74
00:03:39,260 --> 00:03:41,650
we should not get this error anymore
75
00:03:41,650 --> 00:03:45,250
but instead a different error related to our image key
76
00:03:45,250 --> 00:03:46,730
which again makes sense here,
77
00:03:46,730 --> 00:03:48,840
because for posted detail
78
00:03:48,840 --> 00:03:52,210
I'm also accessing post.image here,
79
00:03:52,210 --> 00:03:53,760
that was correct before.
80
00:03:53,760 --> 00:03:58,530
Now it's .image_name, so we should fix this.
81
00:03:58,530 --> 00:04:01,042
And with this fixed, if we now reload,
82
00:04:01,042 --> 00:04:02,683
that's looking better.
83
00:04:03,670 --> 00:04:06,450
So now that is generally working
84
00:04:06,450 --> 00:04:10,330
and we are outputting all that post data again.
85
00:04:10,330 --> 00:04:12,250
But of course, we also have to tag
86
00:04:12,250 --> 00:04:14,760
and we have to author the author model.
87
00:04:14,760 --> 00:04:17,660
And I am outputting the author here for example.
88
00:04:17,660 --> 00:04:20,140
I'm simply out putting post.author like this.
89
00:04:20,140 --> 00:04:24,500
And because in the author model I added str,
90
00:04:24,500 --> 00:04:28,410
this is actually I'm putting my full name, so that's good,
91
00:04:28,410 --> 00:04:32,010
but maybe we wanna make this clickable, that name.
92
00:04:32,010 --> 00:04:34,040
We wanna turn this into a link,
93
00:04:34,040 --> 00:04:37,370
and when you click it, we open the mail client.
94
00:04:37,370 --> 00:04:40,660
And maybe we also wanna show the tags of a post.
95
00:04:40,660 --> 00:04:44,870
Let's say here on that post page, below that title.
96
00:04:44,870 --> 00:04:47,070
And therefore these are the next steps
97
00:04:47,070 --> 00:04:48,883
I wanna take in the next lectures.
7519
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.