All language subtitles for 010 Fetching Posts for Starting Page_en

af Afrikaans
ak Akan
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

So now with our models all set up

and with some data being added for our admin view,

I now wanna make sure that for the front-end of our blog.

So what our users, our visitors see,

we of course also now use that data from the database

with help of the models.

And currently in the views py

we are of course using that dummy data

that all posts list with our dummy dictionaries.

I will now remove that here

or I will at least remove those dummy posts in there

so that we can rule out that data's coming from there.

Because now the goal

is to step-by-step migrated those view functions

to use the models and get the data from there.

And let's start with the function

which is responsible for this starting page

where we also of course have these free block posts.

So this starting page function here.

There are in the past, we basically sorted all posts by date

with that get date help or function,

and then we got the last free posts.

Now, of course, the goal is now to use our model,

and therefor the first step is to import

from the models file here in that directory,

import the post model.

And then in starting page, we can build a query

as we learn it in the Shell over the last sections.

We can reach out to our posts with post objects

and we could get all posts with that all method here.

We could also filter for a post if we wanted to,

but here we don't really want to filter.

We only have two restrictions,

and that's a limit of posts which you wanna fetch,

we only wanna get three posts,

and we wanna order our posts.

Now for ordering, we can call order by,

we can call this on the result of filter,

but we can also call it on the result of all

since we can generally call it on any query set

and all the just returns such a query set.

So here we can also call order by

and then pass in the key by which you wanna order.

And that would be the date here.

So one of the fields in our model,

in this case, in the post model, that date field.

I wanna filter by that and we can filter an ascending order

or in descending order.

And I wanna filter in descending order with a minus here

to make sure that's the post we added the last is on top.

Now that gives us the ordered posts,

but we only wanna get three.

And here's something very neat which we can do with Django,

which we haven't seen up to this point in this course,

which you might have done in your solution still,

and which is absolutely correct.

We cannot also take a slice of that result here

with that default slicing syntax we know from Python,

so by adding square brackets,

and then to take only the first free elements,

we use this slicing syntax.

Now a couple of important annotations here though

from my site.

For one, you might think that this is bad for performance,

because you could think that here Django reaches out

to the database, gets all posts

and then slices all posts in Python.

And that would be very inefficient

because we would needlessly fetch all posts

from the database, even though we only need three,

and then we would filter them in Python.

So we would get way too many results from the database

and then also have a performance hit when we filter

or when we slice these results in Python.

But thankfully this is not what's going on.

Django is smart

and it will actually convert this entire statement here

into a SQL command.

So it will not get all ordered posts,

but it takes this slicing syntax also into account

and actually creates one long query, one long SQL query

based on this entire line where it already slices

when fetching the data from the database.

So it only fetches free results from the database

because of this line.

That's some optimization which Django does for us

behind the scenes automatically.

It is important to know though that for this,

Django does not support negative indexing here.

So -3: as we did it before

would not be supported here.

But that's no problem because since I'm sorting by date

in a descending order already,

I now wanna get the first free dates

because since we're starting with the most recent date,

because of the ordering, I wanna take the first three posts

because that will be the three most recent posts.

So therefore this one line does all they need to do,

it gets us the free latest posts

and therefor I can now get latest posts

and pass them on to posts here to my starting page.

Now, if we save that

and we go back and reload that starting page,

I get an error, failed to look up for key image

in post object.

And that makes sense because in my post model here,

I do have a key named image name,

that's where my image name is stored in.

But in my template, if I have a look at the index html file

for the starting page, there are in this template

in the post then, in that included post,

I am referring to just .image.

This should now .image_name

since that's the field name in the post model.

And we are now working with that post model.

So that's something we need to change and save.

And then we can reload here.

And this now looks way better.

Now those posts are being loaded correctly

and therefore let's now move on to all posts

where of course now currently nothing is showing up

because we haven't updated this method yet.

In case you haven't implemented it on your own yet,

maybe try implementing this page on your own now.

In the next lecture, we're going to do it together again.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.