1 00:00:00,320 --> 00:00:07,060 In the previous video, we already create a home page like this. But this is just a dummy data. 2 00:00:07,060 --> 00:00:12,400 In this video, we are going to create a database in our project and change this 3 00:00:12,400 --> 00:00:15,080 dummy data by data in the database. 4 00:00:15,760 --> 00:00:17,680 Okay let’s just start. 5 00:00:17,780 --> 00:00:19,900 Open the visual studio code. 6 00:00:21,760 --> 00:00:26,580 Here in the models.py file, we will create a device models. 7 00:00:26,580 --> 00:00:30,420 I will just copy and paste the script from my note and explain 8 00:00:30,420 --> 00:00:32,480 to you what the script means. 9 00:00:33,460 --> 00:00:39,780 Here we have a devices class. Every class in models will map to one database table. 10 00:00:39,780 --> 00:00:43,220 So in this case, we create a Device table. 11 00:00:43,220 --> 00:00:56,140 The devices table will have some column. Here we have IP Address, Hostname, Username, Password, SSH Port, and Vendor. 12 00:00:57,680 --> 00:01:02,860 The type of ip address, hostname, username, and password is charfield. 13 00:01:02,940 --> 00:01:05,700 CharField is string in python. 14 00:01:05,700 --> 00:01:09,600 And The type of ssh port will be integerfield. 15 00:01:10,560 --> 00:01:17,680 Here we have a choice for vendor, in this case we have Mikrotik & Cisco. 16 00:01:19,260 --> 00:01:28,140 And here we create a vendor column. The type is CharField and we define the choice here. The choice 17 00:01:28,140 --> 00:01:32,600 is based on vendor choices that we have defined before. 18 00:01:33,620 --> 00:01:41,160 Last, here we define def str. This script is used to show the id and the ip address of device 19 00:01:41,160 --> 00:01:50,160 in the django admin page. If we don’t do this, we will see device object 1,2,3 in the django admin page. 20 00:01:50,340 --> 00:01:55,600 I recommend you to pause this video now and write this code by yourself. 21 00:01:55,700 --> 00:02:00,280 When you have finished writing this code, you can continue the video. 22 00:02:01,200 --> 00:02:09,760 Next, we need to register our models to admin.py, so we can manage the database in the django admin page. 23 00:02:10,420 --> 00:02:13,260 First we need to import the models. 24 00:02:21,460 --> 00:02:24,620 And then we need to register the database. 25 00:02:30,340 --> 00:02:37,060 Because we modify models.py file, so we need to doing a migration in our project. 26 00:02:37,320 --> 00:02:47,120 Migration is used to create a database based on models.py file. Open the command prompt, and stop the project. 27 00:02:48,660 --> 00:02:51,460 First let’s do makemigration 28 00:02:56,920 --> 00:02:59,080 and then do migrate. 29 00:03:08,060 --> 00:03:14,080 Okay we have done migrate the project. Now let’s start the project again. 30 00:03:19,120 --> 00:03:24,000 Let’s open the browser and make sure that our project is still running well. 31 00:03:24,540 --> 00:03:27,940 Okay our project is still running well. 32 00:03:28,240 --> 00:03:34,560 Now let’s go to django admin page. Here we can see django admin page. 33 00:03:34,560 --> 00:03:42,380 We need to login here, but we don’t have a username & password yet. So we need to create it first. 34 00:03:42,540 --> 00:03:44,540 Back to command prompt, 35 00:03:46,100 --> 00:03:47,680 stop the server, 36 00:03:49,820 --> 00:03:51,740 and create a super user. 37 00:03:57,420 --> 00:03:59,380 Type the username. 38 00:03:59,560 --> 00:04:05,400 We can left the email blank, type the password and confirm password. 39 00:04:05,800 --> 00:04:14,740 Here we can just ignore it. Okay we have done creating a new user. Let’s start the project again. 40 00:04:19,140 --> 00:04:25,700 Go back to the browser, refresh, and then login to the django admin page using username 41 00:04:25,700 --> 00:04:28,380 and password that we just created. 42 00:04:30,140 --> 00:04:34,860 Okay now we have successfully login to the django admin page. 43 00:04:35,140 --> 00:04:40,600 Here we can see a Device Table. Let’s create some new device. 44 00:04:46,180 --> 00:04:49,060 Okay we have some devices here. 45 00:04:50,000 --> 00:04:53,900 Now we will modify home.html file. 46 00:04:55,380 --> 00:04:59,260 In the previous video, we just put a dummy data here. 47 00:04:59,400 --> 00:05:05,380 In this video we will modify the script to display the infomartion based on the database. 48 00:05:05,940 --> 00:05:10,020 But before that, we need to edit views.py file. 49 00:05:12,280 --> 00:05:15,720 In the home function, we need to create a context. 50 00:05:16,500 --> 00:05:19,660 First, we need to import device models. 51 00:05:27,720 --> 00:05:32,280 Now let’s create a variable to save all devices in the database. 52 00:05:42,480 --> 00:05:46,480 Let’s also create a variable to save all cisco devices. 53 00:05:58,260 --> 00:06:02,460 And also we need a variable to save all mikrotik devices. 54 00:06:12,600 --> 00:06:20,680 Okay now we have three variable here, which is all_devices, cisco devices, and mikrotik devices. 55 00:06:21,220 --> 00:06:28,020 Next we need to creat a context. Remember that the context must be a dictionary. 56 00:06:28,260 --> 00:06:36,200 The first key will be all_devices, and the value is len (all_device). 57 00:06:36,480 --> 00:06:45,900 Len is a function that return the total of device. Next we also need to devine a key for cisco & mikrotik device. 58 00:06:59,520 --> 00:07:03,800 Last, we need to put this context in the return statement. 59 00:07:04,220 --> 00:07:07,680 Now let’s edit the home.html file. 60 00:07:13,200 --> 00:07:19,760 This is total device, so let’s change this to total_devices. 61 00:07:20,160 --> 00:07:24,540 Remember that we need to use double curly braces if we want to accesss 62 00:07:24,540 --> 00:07:26,360 data from the context. 63 00:07:26,700 --> 00:07:29,620 Let’s change this to cisco devices, 64 00:07:31,400 --> 00:07:35,440 and last, let’s change this to mikrotik devices. 65 00:07:38,780 --> 00:07:44,300 Let’s go to the browser. Refresh. 66 00:07:44,320 --> 00:07:47,580 Here we can see that we have successfully get the 67 00:07:47,580 --> 00:07:50,220 total of cisco & mikrotik devices. 68 00:07:50,420 --> 00:07:57,680 But we failed in total device. Let’s back to the visual studio code and see whats wrong. 69 00:07:58,760 --> 00:08:03,600 Let’s chek in views.py file first, here in the context, 70 00:08:03,600 --> 00:08:06,120 we define all_device. 71 00:08:06,420 --> 00:08:14,700 Now let’s check in home.html file. Look, here I call total_device from the context. 72 00:08:14,840 --> 00:08:19,920 We should call all_devices here. Let’s change this 73 00:08:22,100 --> 00:08:24,040 Go back to the browser, 74 00:08:25,020 --> 00:08:26,180 refresh. 75 00:08:26,480 --> 00:08:30,900 Okay here we can see that we have 5 total devices. 76 00:08:30,900 --> 00:08:38,880 Which is 2 cisco device and 3 mikrotik device. Let’s add a new device in the database. 77 00:08:56,920 --> 00:08:59,880 Here I create a mikrotik device. 78 00:09:02,240 --> 00:09:04,340 Let’s back to our app, 79 00:09:06,340 --> 00:09:07,640 refresh. 80 00:09:08,020 --> 00:09:11,260 Okay now we have 6 total devices, 81 00:09:11,260 --> 00:09:15,960 which is 2 cisco devices and 4 mikrotik devices. 82 00:09:16,400 --> 00:09:22,340 This is the end of this video. In the next video, we are going to create a Device List menu. 83 00:09:22,440 --> 00:09:25,240 Thankyou for watching and see you on the next video!