1 00:00:00,932 --> 00:00:02,404 Hi, welcome back. 2 00:00:02,405 --> 00:00:08,314 Previously we created two Blog Posts, Cats and Dogs. 3 00:00:08,315 --> 00:00:10,580 However, our home page is not working, 4 00:00:10,581 --> 00:00:13,840 so how do we add a home page? 5 00:00:14,450 --> 00:00:16,420 Let's do that in this video. 6 00:00:16,421 --> 00:00:19,492 For now, we are going to keep things simple. 7 00:00:19,493 --> 00:00:23,588 We are going to have just a home page, that says, 8 00:00:23,589 --> 00:00:26,492 this is a home page. But later on we are going 9 00:00:26,493 --> 00:00:33,290 to generate automatically, a list of Blog Posts in the home page. 10 00:00:33,291 --> 00:00:37,470 For now, let's just have a simple HTML page. 11 00:00:37,471 --> 00:00:42,650 So that you practice creating new pages in Django. 12 00:00:43,310 --> 00:00:45,552 So you know more or less how 13 00:00:45,553 --> 00:00:48,752 things work now, but let's repeat that. 14 00:00:48,753 --> 00:00:51,008 So where do we start? 15 00:00:51,009 --> 00:00:53,366 Do we start by creating a module 16 00:00:53,367 --> 00:00:56,426 or a view or an HTML template? 17 00:00:56,427 --> 00:00:59,652 So you may still be confused about where to 18 00:00:59,653 --> 00:01:03,806 start, but I would suggest you follow this approach. 19 00:01:03,807 --> 00:01:07,198 Always think about, the user. 20 00:01:07,199 --> 00:01:11,166 So solve the problem starting from the user. 21 00:01:11,167 --> 00:01:15,672 What the user sees is the HTML, and then 22 00:01:15,673 --> 00:01:19,612 you go down in levels, until you reach the 23 00:01:19,613 --> 00:01:23,074 back end, which is the models or the views. 24 00:01:23,075 --> 00:01:24,668 So let's start to build the 25 00:01:24,669 --> 00:01:27,010 page by thinking about the user. 26 00:01:27,011 --> 00:01:30,326 Therefore you need to create an HTML 27 00:01:30,327 --> 00:01:33,302 template, to show that to the user. 28 00:01:33,303 --> 00:01:35,633 So go to templates and, 29 00:01:35,634 --> 00:01:37,800 [No Audio] 30 00:01:37,801 --> 00:01:40,342 right click over and go to new file. 31 00:01:40,343 --> 00:01:47,082 And then, usually the home page is called index.html. 32 00:01:47,083 --> 00:01:49,348 This is a standard way, but you can 33 00:01:49,349 --> 00:01:51,908 also write any other name that you like. 34 00:01:51,909 --> 00:01:58,590 So I'm going to keep index.hmtl, again the DOC. 35 00:02:00,150 --> 00:02:03,190 Then you have the html tags 36 00:02:03,191 --> 00:02:07,480 and the body tags, like that. 37 00:02:08,570 --> 00:02:11,532 Let's write some static text here. 38 00:02:11,533 --> 00:02:14,040 This is the homepage. 39 00:02:14,570 --> 00:02:18,242 But how can the user get this html template, 40 00:02:18,243 --> 00:02:20,032 what do they do? 41 00:02:20,033 --> 00:02:24,422 Well, they enter a URL, right in their browser. 42 00:02:24,423 --> 00:02:27,008 I mean they could also find that URL on 43 00:02:27,009 --> 00:02:30,262 Google or directly enter that on their browser. 44 00:02:30,263 --> 00:02:31,920 It doesn't matter for us. 45 00:02:32,530 --> 00:02:37,370 But they have to enter a URL somehow on their browser, 46 00:02:37,371 --> 00:02:41,170 which would be like, example.com, that is the home page. 47 00:02:41,171 --> 00:02:48,260 So we need to go to, the urls, of our blog app. 48 00:02:48,870 --> 00:02:53,182 So this urls.py file, not the urls.py 49 00:02:53,183 --> 00:02:56,280 file of mysite, we are done with that. 50 00:02:56,333 --> 00:03:00,722 We entered, we told Django about the urls.py 51 00:03:00,723 --> 00:03:02,988 file of our app and that's all. 52 00:03:02,989 --> 00:03:07,186 We close that now and we work on the app urls. 53 00:03:07,187 --> 00:03:10,688 So the blog urls, which has 54 00:03:10,689 --> 00:03:16,630 this list of path function calls. 55 00:03:16,631 --> 00:03:19,750 So this function is being called with two arguments. 56 00:03:19,751 --> 00:03:22,820 One here, one here, three actually 57 00:03:22,821 --> 00:03:25,170 and a third one in here. 58 00:03:25,171 --> 00:03:28,320 So now we need to add another call. 59 00:03:29,010 --> 00:03:34,230 path, this time is just an empty string. 60 00:03:34,231 --> 00:03:38,920 Before, after the domain name we had the slug. But 61 00:03:38,921 --> 00:03:41,320 now, after the domain name we don't have anything. 62 00:03:41,321 --> 00:03:44,970 So just leave it an empty string there, a comma. 63 00:03:44,971 --> 00:03:46,722 Oh, now we have views. 64 00:03:46,723 --> 00:03:49,388 Okay, so we're moving into our 65 00:03:49,389 --> 00:03:53,058 roadmap, of having a page published. 66 00:03:53,059 --> 00:03:55,356 So views, we want to create 67 00:03:55,357 --> 00:03:58,166 another view now, in views.py. 68 00:03:58,167 --> 00:04:00,816 So let's just write it first 69 00:04:00,817 --> 00:04:03,888 here, we are going to name it HomeView. 70 00:04:03,889 --> 00:04:09,136 So HomeView.as_view, will be the method of that. 71 00:04:09,137 --> 00:04:11,290 So we have to call as_view. 72 00:04:12,050 --> 00:04:13,828 Let's give this a name. 73 00:04:13,829 --> 00:04:16,160 So we maybe use it later. 74 00:04:17,329 --> 00:04:22,019 home_view a string, right? 75 00:04:22,020 --> 00:04:24,500 And then create a view. 76 00:04:24,501 --> 00:04:26,630 [No Audio] 77 00:04:26,631 --> 00:04:38,620 So a new class, class HomeView comes from generic.DetailView, 78 00:04:38,621 --> 00:04:40,668 actually, it shouldn't come from this, but 79 00:04:40,669 --> 00:04:43,000 let's leave it like that for now. 80 00:04:44,330 --> 00:04:48,672 And the model now well, this 81 00:04:48,673 --> 00:04:50,400 page doesn't really have a model. 82 00:04:50,401 --> 00:04:55,712 It doesn't need a model, actually, because 83 00:04:55,713 --> 00:04:59,200 see, this is just a static page. 84 00:04:59,201 --> 00:05:04,458 It's not getting data from a database like blog.html. 85 00:05:04,459 --> 00:05:06,308 You see, in blog.html we 86 00:05:06,309 --> 00:05:08,506 get this data from the database. 87 00:05:08,507 --> 00:05:09,812 This is not the case here. 88 00:05:09,813 --> 00:05:13,970 So you can have views without models. 89 00:05:13,971 --> 00:05:15,732 So all you want to do here 90 00:05:15,733 --> 00:05:19,736 is templates_name equal to home, 91 00:05:19,737 --> 00:05:22,766 sorry, index.html. 92 00:05:22,767 --> 00:05:24,200 So that's one, 93 00:05:24,201 --> 00:05:29,000 [No Audio] 94 00:05:29,001 --> 00:05:31,698 which means that, when the users visit 95 00:05:31,699 --> 00:05:38,912 this URL, this view will call that template to be 96 00:05:38,913 --> 00:05:42,110 rendered in the browser, in the user's browser. 97 00:05:42,111 --> 00:05:47,222 And that's it, make sure to save views.py, make sure to save urls.py, 98 00:05:47,223 --> 00:05:49,500 make sure to save index.html, 99 00:05:49,501 --> 00:05:54,633 [No Audio] 100 00:05:54,634 --> 00:05:56,966 and then try out the page. 101 00:05:58,050 --> 00:06:00,356 So this is the home page and 102 00:06:00,357 --> 00:06:02,950 it seems to have a problem. 103 00:06:02,951 --> 00:06:05,998 So problems are now emerging. 104 00:06:05,999 --> 00:06:10,150 It says that HomeView is missing a QuerySet. 105 00:06:10,151 --> 00:06:12,900 So you need to define the HomeView model. 106 00:06:13,590 --> 00:06:18,380 The problem here is that in views, this is not 107 00:06:18,381 --> 00:06:25,346 the correct View type to use, with Views that simply 108 00:06:25,347 --> 00:06:28,992 render a template without getting data from a model. 109 00:06:28,993 --> 00:06:31,232 Therefore, in this case, what you want to use 110 00:06:31,233 --> 00:06:34,033 instead is a TemplateView, 111 00:06:34,034 --> 00:06:37,633 [No Audio] 112 00:06:37,633 --> 00:06:41,658 save and refresh. 113 00:06:41,659 --> 00:06:44,080 And, This is the homepage! it's working. 114 00:06:45,250 --> 00:06:48,800 You can see it as I zoom it in, right. 115 00:06:49,410 --> 00:06:52,458 So that is how you can add more pages. 116 00:06:52,459 --> 00:06:54,808 And you can also now understand 117 00:06:54,809 --> 00:06:57,550 the difference between different view types. 118 00:06:57,551 --> 00:07:00,520 So DetailView and TemplateView. 119 00:07:00,521 --> 00:07:03,822 TemplateView means, when you only need to render 120 00:07:03,823 --> 00:07:06,440 a template without getting data from a model. 121 00:07:07,290 --> 00:07:09,692 And this is a more complex View, which gets 122 00:07:09,693 --> 00:07:15,130 data from models, and passes them to templates. 123 00:07:15,131 --> 00:07:17,708 In the next video, I'm going to show you how 124 00:07:17,709 --> 00:07:22,972 to use yet another view type, which is basically used 125 00:07:22,973 --> 00:07:28,466 to create a list of objects, a list of blogs. 126 00:07:28,467 --> 00:07:32,133 Blog Posts in our case, talk to you in the next video.