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