In the previous video, we already learned about urls and views in Django. 2
Now in this video, we are going to learn about templates in Django. Okay let’s start. Open the visual studio code 3
Here in views.py we can see that the function just return simple html expressions. 4
If we use this method, we will get a problem when we want to render complex page. 5
We can't put large html syntax here. That’s not effective. This is best practice to work with templates. 6
So, if we work in the team, the front end developer can work easily without touch the 7
Django code, they can focus on html, css, and javascript code. 8
Okay, I will show you how to use templates in Django. First, we need to define the directory 9
where we will save our templates in settings.py, in the Templates section. 10
Now let’s create templates directory. Please notice that templates directory must in the 11
same level as our project and app directory. 12
Okay now we have templates directory 13
Now let’s modify views.py file. Here we will not return the html expressions anymore 14
We will return render and the template files. 15
Here we can see the hello_world function 16
will return hello_world.html. 17
Now let’s create hello_world.html inside templates directory. 18
You can write a simple html file in here, in this case I just want to print Hello World. 19
Now let’s go to browser, navigate to app1/hello_world. 20
Here we can see that we get hello world. 21
This display is generated by our template. To make sure, let’s add some html syntax 22
in the template file. Let's say we want to add Welcome to Django. 23
Back to browser, refresh, and here we can see that we get Hello world 24
and Welcome to Django. Okay this is mean that our template file is work! 25
Now let’s manipulate all of function inside views.py to use templates. We will use a.html 26
for function A, and b.html for function B. 27
Now let’s create a.html & b.html 28
Let’s just copy and paste from hello_world.html to a.html & b.html. 29
For b.html, I want to display This is B, and for a.html, I want display This is A. 30
Let’s back to the browser. Let’s go to a, here we can see that we get This is A, 31
let’s go to b, and here we can see that we get This is B. 32
Congratuliation! Now we are able to use templates in Django. 33
In the next video we are going to learn about context. Thank you for watching and see you on the next video.