Afrikaans
Akan
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
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.