In the previous video, we already create a verify config menu. 2
Now in this video, we are going to create log menu. 3
Okay let’s just start. Open the visual studio code. First, let’s create a models for Log. 4
Here I will copy paste from my note. 5
Here we have some column in the log models. Target will be used to store the ip address of the router. 6
Action will be configure or verify config. And status will be success or failed. 7
We have time here, this column will show us when the action is executed. 8
Finally we have a message here. This column will show us the message if the action is failed. 9
Because we doing a modification in the models.py file, so we need to doing a migration. 10
Open the command prompt, stop the server, and do a makemigration. 11
Next let’s do a migrate. 12
Okay, now let’s run the server again 13
Now let’s go to views.py file. Here we will doing a manipulation in the configure & verify_config function. 14
If we see here, the script will try to login to the device and send the command. 15
When try to login to the router, there is a chance that we got an error, either we use 16
a wrong IP Address or may be we have a wrong username and password. 17
So we need to use try in here. 18
And then here, we need to use except. 19
Except block will be executed if we got an error in the try block. Here we will write a script to add data to 20
the log database that the action is failed. 21
I will just copy and paste the script from my note. 22
Here we add a data to the log database. But we need to import Log first. 23
Here the target will be the ip address of the router, dev is the variable that we already defined before. 24
Then the action will be configure, because we doing a manipulation in the configure function. 25
The status will be error. The time will be datetime.now, and the message will be e, 26
which is the variable that save the exception message. 27
Here we can see that we use datetime function, so we need to import it first. 28
Until now, we only add data to log database when the program is failed, 29
but if the program is success, we don’t add data to log database. Now let’s write a script to add data to 30
the log database when the program is success. We can just copy and paste this script. 31
Let’s change the status to success, 32
and the message is “no error”, because the program is running successfully. 33
Okay now we have done in the configure function. Let’s copy and paste this script to the verify_config function. 34
Let’s change the action to verify config. 35
Now, let’s create a page to show the log. Open urls.py file, here let’s create a new url, let’s say log. 36
And this url will be redirected to log function inside views.py. 37
Now let’s create log function in the views.py. 38
Let’s create a variable to save all of logs from the database. 39
Next let’s create a context to pass the variable to the template. 40
And finally return the template file and the context. 41
Now let’s create the template file. I already upload the log.html file in the resources section. 42
So you can just download it and put it in the templates directory. 43
Okay now we have log.html file here. Open the browser, and navigate to 127.0.0.1:8000/log. 44
Okay now we have log page here. But we don’t have any log yet, because we don’t do any action for now. 45
In the next video, we are going to test our application, so we will get a log in here. 46
Thankyou for watching and see you on the next video!