1 00:00:00,410 --> 00:00:04,612 Hello, previously we created a view, which is a 2 00:00:04,613 --> 00:00:08,890 class that connects the model with the template. 3 00:00:08,891 --> 00:00:10,676 Now in this video, I want to enter 4 00:00:10,677 --> 00:00:13,882 some dummy data in our database table. 5 00:00:13,883 --> 00:00:16,948 So our database file is db.sqlite3, 6 00:00:16,949 --> 00:00:19,508 and it has several tables inside. 7 00:00:19,509 --> 00:00:21,972 I want to use the DB browser for 8 00:00:21,973 --> 00:00:25,050 SQLite and then go to Open Database. 9 00:00:25,051 --> 00:00:28,268 And then this is a root directory and go 10 00:00:28,269 --> 00:00:31,240 to db.sqlite3 and double click there. 11 00:00:32,650 --> 00:00:34,950 Then you want to go to Browse 12 00:00:35,530 --> 00:00:42,190 Data, and find the blog_post table. 13 00:00:42,191 --> 00:00:45,062 So these are the fields of the database table. 14 00:00:45,063 --> 00:00:46,816 Do you want to expand them a 15 00:00:46,817 --> 00:00:50,070 little bit, to make them more visible? 16 00:00:50,071 --> 00:00:56,564 And date_created, slug, status of the post. 17 00:00:56,565 --> 00:01:02,850 So draft or published and the author_id, which could be a number. 18 00:01:02,851 --> 00:01:05,626 So the first author that will be created 19 00:01:05,627 --> 00:01:07,944 has an id of one and so on. 20 00:01:07,945 --> 00:01:11,390 So let us manually enter a new table row. 21 00:01:11,391 --> 00:01:13,240 Of course, as I told you, these 22 00:01:13,241 --> 00:01:15,422 should be entered through the Admin interface. 23 00:01:15,423 --> 00:01:17,804 But for now, just for testing, we want 24 00:01:17,805 --> 00:01:20,572 to go to this button, press that, and 25 00:01:20,573 --> 00:01:24,658 that will insert a new, data, table row. 26 00:01:24,659 --> 00:01:27,554 So the id was automatically generated. 27 00:01:27,555 --> 00:01:30,208 So for the first row it will be 1, for the 28 00:01:30,209 --> 00:01:34,928 second, it will auto increment to 2 and so on. 29 00:01:34,929 --> 00:01:36,352 So let's put something for the 30 00:01:36,353 --> 00:01:39,470 title like Dogs and content, 31 00:01:39,471 --> 00:01:41,570 Dogs are good. 32 00:01:41,571 --> 00:01:44,282 That's enough for now. date_created, 33 00:01:44,283 --> 00:01:48,068 this should be in format like 2022, 12 for 34 00:01:48,069 --> 00:01:51,860 the month, 10 for the day of the month. 35 00:01:51,861 --> 00:01:57,512 slug like that dogs, and Django will 36 00:01:57,513 --> 00:02:01,774 take care of building the complete URL. 37 00:02:01,775 --> 00:02:05,468 So example, add .com, it will add a slash and 38 00:02:05,469 --> 00:02:08,204 then it will add the slug dogs, after 39 00:02:08,205 --> 00:02:14,706 the slash. status, this was draft or published. 40 00:02:14,707 --> 00:02:21,260 So, in here, you see we have zero or ones. 41 00:02:23,310 --> 00:02:27,984 So let's put 1for published. author_id, 42 00:02:27,985 --> 00:02:33,492 the first user of the database, we 43 00:02:33,493 --> 00:02:35,578 did create already a super user. 44 00:02:35,579 --> 00:02:40,610 So that user has the id 1, right. 45 00:02:40,611 --> 00:02:42,936 Once you have done that, you want to go 46 00:02:42,937 --> 00:02:48,376 to write changes to the database file, all right? 47 00:02:48,377 --> 00:02:50,168 And then you can just close the 48 00:02:50,169 --> 00:02:54,344 database and the row is there. 49 00:02:54,345 --> 00:02:58,236 Now, let's connect that slug, that 50 00:02:58,237 --> 00:03:03,640 URL to our views BlogView. 51 00:03:04,730 --> 00:03:06,972 To do that you need to go to your 52 00:03:06,973 --> 00:03:10,992 blog folder, and right click and go to New 53 00:03:10,993 --> 00:03:17,286 File and create a urls.py file, urls. 54 00:03:17,287 --> 00:03:18,928 And in here you want to import 55 00:03:18,929 --> 00:03:24,210 from the current location, import views. 56 00:03:24,211 --> 00:03:27,150 So we are importing the views module. 57 00:03:28,130 --> 00:03:30,340 It's a file, but when you import things, 58 00:03:30,341 --> 00:03:33,646 that is a module, it becomes a module. 59 00:03:33,647 --> 00:03:35,992 That's how we call it. 60 00:03:35,993 --> 00:03:39,112 So Views now is available here, and we also 61 00:03:39,113 --> 00:03:43,246 need to import something else from django.urls. 62 00:03:43,247 --> 00:03:49,474 A module urls is another module of django import path. 63 00:03:49,475 --> 00:03:52,799 And we need this Path now, which is a function, 64 00:03:54,366 --> 00:04:03,100 to create a urls, urlpatterns list. 65 00:04:04,910 --> 00:04:06,912 So I'm splitting that because this 66 00:04:06,913 --> 00:04:09,542 may contain different urlpatterns. 67 00:04:09,543 --> 00:04:11,524 So the first pattern we are interested 68 00:04:11,525 --> 00:04:14,640 about is created through the path function. 69 00:04:15,250 --> 00:04:17,149 So what is a pattern? 70 00:04:17,730 --> 00:04:23,518 Well, it is slug, a colon and slug 71 00:04:23,519 --> 00:04:28,574 again, with these opening and closing brackets. 72 00:04:28,575 --> 00:04:32,766 So when the user visits a certain URL, 73 00:04:32,767 --> 00:04:35,208 let's say, let me write down something here. 74 00:04:35,209 --> 00:04:40,242 For example, example.com, slug 75 00:04:40,243 --> 00:04:44,012 of course they write like dogs, but dogs, we are 76 00:04:44,013 --> 00:04:48,480 telling Django to look for dogs in the slug field. 77 00:04:48,481 --> 00:04:52,966 So Django will go to our table, database table 78 00:04:52,967 --> 00:04:57,280 and it will search for, in each row for 79 00:04:57,281 --> 00:05:01,812 dogs, in each cell of slug actually. 80 00:05:01,813 --> 00:05:03,930 So in each row of the slug 81 00:05:03,931 --> 00:05:06,554 field, it will search for dogs. 82 00:05:06,555 --> 00:05:14,006 And if it finds a row that has dogs, it's going to 83 00:05:14,007 --> 00:05:20,298 execute BlogView, delete the parentheses, 84 00:05:20,299 --> 00:05:28,766 BlogView.as_view, so execute that class as a view. 85 00:05:29,210 --> 00:05:31,324 And there's also something else we need to 86 00:05:31,325 --> 00:05:33,132 give here which we will use later. 87 00:05:33,133 --> 00:05:37,164 It's a name, which should be a 88 00:05:37,165 --> 00:05:40,620 string and let's say blog_view. 89 00:05:41,790 --> 00:05:43,856 This is a name we may need later. 90 00:05:43,857 --> 00:05:46,704 So it's a good idea to have it here now. 91 00:05:46,705 --> 00:05:49,280 So I hope you are following so far. 92 00:05:49,281 --> 00:05:52,586 We also need to do something else regarding URLs. 93 00:05:52,587 --> 00:06:00,852 You see that, there is another urls.py file, but it is inside my 94 00:06:00,853 --> 00:06:03,608 site, which contains the configurations of the 95 00:06:03,609 --> 00:06:06,430 Django project, not the Django apps. 96 00:06:06,431 --> 00:06:10,136 So this file, looks like this already, 97 00:06:10,137 --> 00:06:12,950 it's built by Django automatically. 98 00:06:12,951 --> 00:06:17,212 And, here is, this one 99 00:06:17,213 --> 00:06:19,810 here is also a URL patterns variable, 100 00:06:19,811 --> 00:06:23,842 just like this one in here, urlpatterns, urlpatterns. 101 00:06:23,843 --> 00:06:29,950 But this now, wants to know about 102 00:06:29,951 --> 00:06:33,088 the urls of the Blog app. 103 00:06:33,089 --> 00:06:35,408 So with each app you create, you want 104 00:06:35,409 --> 00:06:38,272 to declare the urlpattern in here. 105 00:06:38,273 --> 00:06:40,176 So after the comma we create 106 00:06:40,177 --> 00:06:43,070 another listitem which is path. 107 00:06:44,370 --> 00:06:47,748 This time it has to be like this. 108 00:06:47,749 --> 00:06:52,634 So an empty string which denotes the home directory. 109 00:06:52,635 --> 00:06:56,136 So when the home directory is visited and then 110 00:06:56,137 --> 00:06:59,688 something else, after the slash is written by the 111 00:06:59,689 --> 00:07:02,942 user in the address bar of the browser. 112 00:07:02,943 --> 00:07:07,170 And this something else is defined by the blogs. 113 00:07:07,171 --> 00:07:09,564 blog is the name of the app. 114 00:07:09,565 --> 00:07:16,066 So blog, that folder name comes in here, .urls. 115 00:07:17,370 --> 00:07:23,584 urls is the name of the urls.py file of the blog app. 116 00:07:23,585 --> 00:07:25,850 So blog.urls. 117 00:07:26,750 --> 00:07:29,040 You could also write something else here. 118 00:07:29,041 --> 00:07:32,933 So you could write my blogs for example. 119 00:07:33,890 --> 00:07:37,386 And in that case. Django would be expecting 120 00:07:37,387 --> 00:07:45,970 users, not example.com/dogs, but example.com/ myblogs/dogs. 121 00:07:46,550 --> 00:07:49,910 But we don't want that, so let's keep it like that. 122 00:07:49,911 --> 00:07:52,050 example.com/dogs 123 00:07:53,910 --> 00:07:58,396 Now we get this error, include is not defined and we need to import 124 00:07:58,397 --> 00:08:02,300 it from django.urls import path and include 125 00:08:02,301 --> 00:08:04,331 [No Audio] 126 00:08:04,332 --> 00:08:05,333 save. 127 00:08:05,334 --> 00:08:07,690 [No Audio] 128 00:08:07,691 --> 00:08:10,246 Also make sure to save all the files. 129 00:08:10,247 --> 00:08:11,824 So save this as well. 130 00:08:11,825 --> 00:08:15,100 Ctrl+S, save the views as well. 131 00:08:15,630 --> 00:08:18,598 So everything you see with that black dot in Visual 132 00:08:18,599 --> 00:08:21,236 Studio Code, that means it hasn't been saved yet. 133 00:08:21,237 --> 00:08:24,160 Now, let's see what errors we get. 134 00:08:25,730 --> 00:08:29,044 type object 'BlogView' has no attribute 'as_view'. 135 00:08:29,045 --> 00:08:30,964 So see what's going on here. 136 00:08:30,965 --> 00:08:34,073 This happens in this file. 137 00:08:34,074 --> 00:08:40,233 So urls.py, blog/urls.py, which is here, hmm. 138 00:08:40,234 --> 00:08:44,510 So this class does not have that attribute. 139 00:08:44,511 --> 00:08:45,980 Let's take a look. 140 00:08:45,981 --> 00:08:48,012 This class, of course it doesn't have that 141 00:08:48,013 --> 00:08:50,636 attribute, it only has model and template_name 142 00:08:50,637 --> 00:08:55,596 attributes. No methods called as_view, 143 00:08:55,597 --> 00:09:01,046 that is because, I forgot to inherit from a specialized 144 00:09:01,047 --> 00:09:04,166 view class ,which we need to import. 145 00:09:05,566 --> 00:09:13,876 from django, so from django views import generic and then 146 00:09:13,877 --> 00:09:20,000 from generic, we can access the DetailView 147 00:09:20,610 --> 00:09:24,400 Detail, not DeleteView, DetailView class. 148 00:09:25,190 --> 00:09:27,330 So delete the parentheses. 149 00:09:29,270 --> 00:09:31,960 So that means this class actually 150 00:09:31,961 --> 00:09:36,100 has an as_view method inside. 151 00:09:37,770 --> 00:09:40,630 So you can right click and Go to Definition. 152 00:09:41,450 --> 00:09:44,674 And of course this does not have any methods 153 00:09:44,675 --> 00:09:47,922 here, but it inherits from some other methods. 154 00:09:47,923 --> 00:09:50,608 So if we go to BaseDetailView, go 155 00:09:50,609 --> 00:09:56,720 to definition, this is also inheriting from these two. 156 00:09:56,721 --> 00:10:02,084 So maybe View has that, as_view function. 157 00:10:02,085 --> 00:10:04,670 Yeah, now we found it, so that's the method. 158 00:10:05,810 --> 00:10:09,860 So there's a chain of inheritance and we have access 159 00:10:09,861 --> 00:10:17,200 to that method now, from our BlogView class, great. 160 00:10:17,201 --> 00:10:19,166 [No Audio] 161 00:10:19,167 --> 00:10:20,800 Great, save this file 162 00:10:20,801 --> 00:10:22,833 [No Audio] 163 00:10:22,834 --> 00:10:26,920 and now it's time to try out our app. 164 00:10:27,530 --> 00:10:30,070 And you want to go to this URL. 165 00:10:31,130 --> 00:10:33,730 So, this is to be expected. 166 00:10:33,731 --> 00:10:36,892 So the homepage is not configured yet, we 167 00:10:36,893 --> 00:10:41,142 don't have any URL pattern that, tell Django 168 00:10:41,143 --> 00:10:44,910 what to do when people visit the homepage. 169 00:10:44,911 --> 00:10:51,814 So what you should look instead is, one of your blogs. 170 00:10:51,815 --> 00:10:57,572 So currently we have dogs, the dogs slag, you remember that, 171 00:10:57,573 --> 00:11:02,554 I inserted it into the table and we get this error, 172 00:11:02,555 --> 00:11:08,050 TemplateDoesNotExist, an easy error to fix at /dogs. 173 00:11:09,030 --> 00:11:13,272 So Django recognized that we do have a 174 00:11:13,273 --> 00:11:15,868 dogs entry in the database, because if you 175 00:11:15,869 --> 00:11:19,590 tried for cats, you'd get another error. 176 00:11:20,330 --> 00:11:23,320 So this time you'd get, Page not found. 177 00:11:24,490 --> 00:11:28,990 And Django tried these patterns and 178 00:11:28,991 --> 00:11:31,862 didn't find any slug for cats. 179 00:11:31,863 --> 00:11:34,096 So dogs is a different thing, 180 00:11:34,097 --> 00:11:36,700 but the Template is not found. 181 00:11:36,701 --> 00:11:38,910 [No Audio] 182 00:11:38,911 --> 00:11:44,084 So blog.html, this usually is a problem with 183 00:11:44,085 --> 00:11:49,140 miscommunication with Django, so Django is not being able 184 00:11:49,141 --> 00:11:55,742 to find the path of blog.html. 185 00:11:55,743 --> 00:12:00,420 So let's check, blog.html is correct here. 186 00:12:01,990 --> 00:12:06,018 So the View has the name correctly, and then let's 187 00:12:06,019 --> 00:12:07,266 look at the settings, 188 00:12:07,267 --> 00:12:11,400 [No Audio] 189 00:12:11,401 --> 00:12:14,440 templates, the name is correct here. 190 00:12:15,290 --> 00:12:19,094 So this looks fine but, if you scroll 191 00:12:19,095 --> 00:12:23,770 up in here, you have this TEMPLATES variable. 192 00:12:25,550 --> 00:12:29,910 And this now has some parameters about TEMPLATES, 193 00:12:29,911 --> 00:12:33,033 and one of these parameters is DIRS. 194 00:12:33,330 --> 00:12:38,884 So these are keys of that dictionary, BACKEND, DIRS key. 195 00:12:38,885 --> 00:12:42,100 The DIRS key expects a value and currently 196 00:12:42,101 --> 00:12:44,152 it's an empty list, but you need to 197 00:12:44,153 --> 00:12:48,930 declare here, the list of TEMPLATE directories. 198 00:12:49,510 --> 00:12:52,184 So in this case we only have one because 199 00:12:52,185 --> 00:12:55,836 you could have more but, we have one. 200 00:12:55,837 --> 00:13:01,033 So we have to declare that in there, save 201 00:13:01,034 --> 00:13:03,166 [No Audio] 202 00:13:03,167 --> 00:13:06,716 and we get this other error, TEMPLATES_DIR is not defined of course, 203 00:13:06,717 --> 00:13:12,304 because we are using it in here, before we defined it. 204 00:13:12,305 --> 00:13:19,654 So cut that from there, and put it above its usage. 205 00:13:19,655 --> 00:13:26,933 So above thisTEMPLATES, variable output it just here, 206 00:13:26,934 --> 00:13:29,233 [No Audio] 207 00:13:29,234 --> 00:13:35,098 save, and our server was interrupted. 208 00:13:35,099 --> 00:13:36,644 So we want to execute again, 209 00:13:36,645 --> 00:13:39,590 python manage.py runserver. 210 00:13:39,591 --> 00:13:41,352 Let's see what we get this time. 211 00:13:41,353 --> 00:13:46,566 Go to the browser, and whoop, we get the blog. 212 00:13:48,001 --> 00:13:49,884 Dogs, Dogs are good! 213 00:13:49,885 --> 00:13:52,098 and ardit, the name of the author. 214 00:13:52,099 --> 00:13:53,400 It's working fine. 215 00:13:53,930 --> 00:13:56,920 So that concludes this video. 216 00:13:57,790 --> 00:14:01,100 So let me wrap it up so you know where you stand. 217 00:14:01,101 --> 00:14:03,310 [No Audio] 218 00:14:03,311 --> 00:14:11,258 So, so far we are able to address the user URL concerns. 219 00:14:11,259 --> 00:14:15,140 So when they enter example.com/dogs, for example, 220 00:14:15,141 --> 00:14:19,970 this urls.py is being triggered, and this 221 00:14:19,971 --> 00:14:24,530 deploys the views and the models. 222 00:14:25,910 --> 00:14:28,100 So it connects the two together, 223 00:14:28,630 --> 00:14:33,490 to query data from the database. 224 00:14:35,370 --> 00:14:39,458 And those data are injected into these, variables 225 00:14:39,459 --> 00:14:42,866 with curly brackets in the HTML Template. 226 00:14:42,867 --> 00:14:49,170 And of course, this then, these are rendered in the browser for the user. 227 00:14:49,171 --> 00:14:52,268 And as you saw, this time for now, I 228 00:14:52,269 --> 00:14:55,138 just entered some data manually using a third party 229 00:14:55,139 --> 00:14:59,610 program, that accesses the SQLite 3 databases. 230 00:14:59,611 --> 00:15:03,012 But normally as you see in the next videos, 231 00:15:03,013 --> 00:15:07,274 we want to get data through an admin interface. 232 00:15:07,275 --> 00:15:09,924 So this is what we're going to do next. 233 00:15:09,925 --> 00:15:12,042 We are going to create the admin 234 00:15:12,043 --> 00:15:15,800 interface, and enter some data in there, see you.