Hi, welcome back.
Previously we created two Blog Posts, Cats and Dogs.
However, our home page is not working,
so how do we add a home page?
Let's do that in this video.
For now, we are going to keep things simple.
We are going to have just a home page, that says,
this is a home page. But later on we are going
to generate automatically, a list of Blog Posts in the home page.
For now, let's just have a simple HTML page.
So that you practice creating new pages in Django.
So you know more or less how
things work now, but let's repeat that.
So where do we start?
Do we start by creating a module
or a view or an HTML template?
So you may still be confused about where to
start, but I would suggest you follow this approach.
Always think about, the user.
So solve the problem starting from the user.
What the user sees is the HTML, and then
you go down in levels, until you reach the
back end, which is the models or the views.
So let's start to build the
page by thinking about the user.
Therefore you need to create an HTML
template, to show that to the user.
So go to templates and,
[No Audio]
right click over and go to new file.
And then, usually the home page is called index.html.
This is a standard way, but you can
also write any other name that you like.
So I'm going to keep index.hmtl, again the DOC.
Then you have the html tags
and the body tags, like that.
Let's write some static text here.
This is the homepage.
But how can the user get this html template,
what do they do?
Well, they enter a URL, right in their browser.
I mean they could also find that URL on
Google or directly enter that on their browser.
It doesn't matter for us.
But they have to enter a URL somehow on their browser,
which would be like, example.com, that is the home page.
So we need to go to, the urls, of our blog app.
So this urls.py file, not the urls.py
file of mysite, we are done with that.
We entered, we told Django about the urls.py
file of our app and that's all.
We close that now and we work on the app urls.
So the blog urls, which has
this list of path function calls.
So this function is being called with two arguments.
One here, one here, three actually
and a third one in here.
So now we need to add another call.
path, this time is just an empty string.
Before, after the domain name we had the slug. But
now, after the domain name we don't have anything.
So just leave it an empty string there, a comma.
Oh, now we have views.
Okay, so we're moving into our
roadmap, of having a page published.
So views, we want to create
another view now, in views.py.
So let's just write it first
here, we are going to name it HomeView.
So HomeView.as_view, will be the method of that.
So we have to call as_view.
Let's give this a name.
So we maybe use it later.
home_view a string, right?
And then create a view.
[No Audio]
So a new class, class HomeView comes from generic.DetailView,
actually, it shouldn't come from this, but
let's leave it like that for now.
And the model now well, this
page doesn't really have a model.
It doesn't need a model, actually, because
see, this is just a static page.
It's not getting data from a database like blog.html.
You see, in blog.html we
get this data from the database.
This is not the case here.
So you can have views without models.
So all you want to do here
is templates_name equal to home,
sorry, index.html.
So that's one,
[No Audio]
which means that, when the users visit
this URL, this view will call that template to be
rendered in the browser, in the user's browser.
And that's it, make sure to save views.py, make sure to save urls.py,
make sure to save index.html,
[No Audio]
and then try out the page.
So this is the home page and
it seems to have a problem.
So problems are now emerging.
It says that HomeView is missing a QuerySet.
So you need to define the HomeView model.
The problem here is that in views, this is not
the correct View type to use, with Views that simply
render a template without getting data from a model.
Therefore, in this case, what you want to use
instead is a TemplateView,
[No Audio]
save and refresh.
And, This is the homepage! it's working.
You can see it as I zoom it in, right.
So that is how you can add more pages.
And you can also now understand
the difference between different view types.
So DetailView and TemplateView.
TemplateView means, when you only need to render
a template without getting data from a model.
And this is a more complex View, which gets
data from models, and passes them to templates.
In the next video, I'm going to show you how
to use yet another view type, which is basically used
to create a list of objects, a list of blogs.
Blog Posts in our case, talk to you in the next video.