In the previous video, we learned about the Django basic concept. And In this video we 2
and in this will learn about urls and views deeply. Later, we will create some page in our project 3
so we can get better understanding about urls and views in Django. 4
But before that, let’s review our code from previous video first. 5
This is the ulrs.py file inside our first_project directory, here we can see that we have admin 6
urls and root url. The root urls is redirected to app1.urls, which is urls.py file inside 7
app1, let’s take a look to that file. 8
Here we can see that the root urls is redirected to hello_world function inside views. 9
Let's take a look at views.py file, here we can see that hello_world function is returning 10
simple html expressions “Selamat datang di Django” 11
Let’s open our browser and navigate to 127.0.0.1:8000. Here we get Selamat datang di Django. 12
Now let’s do some experiments. 13
Back to visual studio code and manipulate urls.py file inside first_project directory. 14
Let’s add app1 here, don’t forget to always add / in the end of the url. 15
Let’s back to the browser, refresh, here we get a 404 error. We can see here that the 16
available url is admin and app1, Now let’s try to add app1 in the url. 17
Here we can see that now we get Selamat datang di Django. 18
Let’s do another experiment, go back to vscode, and edit urls.py file inside app1 19
directory. Let’s add hello_world here. Don’t forget to always add / in the end of the url. 20
Let’s back to browser and refresh. Here we can see that we get 404 error, and we also 21
see that the available url is admin and app1/hello_world. That’s why when we try to access only app1 22
we get a 404 error. Now let’s try to add hello_world in the url. Okay Cool ! now we 23
get Selamat datang di Django. 24
Okay, Let’s do an experiment again, let’s try to add a new url. Here in the urls.py 25
file inside app1 directory, let’s add a new url, let’s say a. This is just for experiment, 26
you can put whatever you want here. This url will be redirected to a function inside views 27
Now we need to create a function in the views, go to views, and here let’s add a function. 28
Let’s say we want to display “This is A”. 29
Let’s back to browser, navigate to 127.0.0.1:8000/app1/a. Okay now we get “This is A” 30
Let’s try to add one more url, 31
let’s say we want to add b. 32
This url will be redirected to b function inside views. 33
Let’s create b function inside views.py file. Let’s say we want to display “This is B" 34
Back to browser again, change a with b, and here we are. Now we get “This is B” 35
Okay now let’s try to test all of our url, let’s change b to hello_world. 36
Okay we get Selamat datang di Django. Change hello_world to a. Okay we get “This is A”, and let’s 37
change again to b. Okay we get “This is B”. 38
Okay I think we already done some experiment and I hope you guys understand about urls 39
and views concept in Django. If you think that you are not fully understand the concept, 40
I recommend you to rewatch this video. Or if you have further question, you can ask 41
me and I will do my best to answer all of your questions. 42
Thankyou for watching and see you on the next video!