Hi, in this video we are going to add the home page
and show a list of Blog Posts in that home page.
So let's start again from the user's point of view.
So we want to have for the user, an index.html template.
With a !DOCTYPE, html tags and body tags.
Then, what do we put in here?
Well, so tags with curly
brackets and percentage symbols. And then,
this time what we do is, a for loop for
post in post_list. And then we do something here and then
we end that loop,
[No Audio]
endfor. Inside here we do something else.
I'll explain you what post_list
is, and what post is also.
But basically then we have the variables here,
which could be post.title and post.content.
And maybe no post.content would be too
much to show, but we could say author.
So this will be just a list of blocks.
We don't want to show the content in
that list, because it would be too much.
So post.title, post.author.
Now, what is post_list?
post_list is a view, which we
need to define in views.py.
So yet another view class, class PostList.
So the name has to be in the same
format, so to say as this one in here.
So we used post_list
in here, with lowercase letters.
Now we need to use Post in Camel case list.
So P and L are uppercase, so
that inherits from generic.ListView.
[No Audio]
So this is yet another type of view.
We had DetailView to render HTML
templates, that get data from a model.
We had a TemplateView to render
HTML templates, without data from a model.
And we have this ListView, which is specialized in
rendering multiple data rows, in this case multiple Posts.
Now, this type of class expects a queryset variable,
which should be equal to Post.
Post is the model.
So be careful here.
It's not the View, it's not
BlogView, it's not AboutView.
Of course it's the model of blogs
which you can find, inside models.py.
Suppose that model is the one we refer here.
What we're trying to do here is, we're
trying to query the objects, so the rows
of that post model. Without the parentheses,
we want to apply a filter and get, maybe get all the
objects, maybe apply a filter such as status equal to 1,
which stands for, let me show it to you.
So status, we are talking about this field.
So we want those Posts, those Blog Posts
which have a value of one for status.
So STATUS has either 0 or 1.
So 1 stands for Publish, 0 stands for Draft.
So we want to show there
only those Posts, which are Published.
So a status value of 1.
And perhaps you want to order them by what field?
So we have date_created, perhaps.
[No Audio]
So date_created.
So that is a relationship with the
modules and the relationship with the templates,
now is template_name equal to index.html, alright.
So we built the template, which looks like this.
We built the view, but we need
to do something with the urls also.
So we have one item here, two items here.
We need a comma here and add a
third item, which is the home page.
So an empty string.
And we want to get from views, we want
to get, for the Postlist vew, PostList.
PostList is a class, so we want to get it as a view.
So call the method as_view from PostList and
give it a name such as home.
Now I think we are ready to try out
the browser and go and visit the home page.
But first make sure you save everything.
This one, the urls.py file, the index.html file and the views.py file.
Save those and see if the app is running,
Yeah, it's running.
So visit the home page and, it's working.
So Dogs ardit, Cats ardit.
Yes, these are the two Blog Posts,
not very appealing, but you can
change that with some HTML.
For example, you may want to put the title inside h2 tags
here, and the author perhaps in p tags.
[No Audio]
So in here,
[No Audio]
save,
[No Audio]
reload and it looks a bit better.
Now the ordering of this, is that, the older Posts are
on top and the newer posts are at the end.
So if you want to change that, it's very easy to do.
Just go over to views.py and
you want to change the queryset, the order by, by just adding a minus.
So this is the Django syntax, and then refresh here,
and Cats will be on top.
Now, the newest Blog Post.
So this is looking good, but I think
there are two things missing in here.
One is that each of these should have a link that
the user can click, and go to that particular Blog Post.
And second, what is missing here is some styling.
So this looks pretty awful.
We need to apply some styling and we're going to
use Bootstrap for that, which is a CSS library.
And it just makes things, so web pages look much better.
So next we are going to add a URL
here, which has to do with URL tags.
So you're going to learn how to use URL tags.
And then next, we add some Bootstrap styling, see you.