1 00:00:00,660 --> 00:00:05,900 In the previous video, we already learned about urls and views in Django. 2 00:00:06,080 --> 00:00:16,360 Now in this video, we are going to learn about templates in Django. Okay let’s start. Open the visual studio code 3 00:00:19,680 --> 00:00:26,840 Here in views.py we can see that the function just return simple html expressions. 4 00:00:27,300 --> 00:00:33,100 If we use this method, we will get a problem when we want to render complex page. 5 00:00:33,480 --> 00:00:43,320 We can't put large html syntax here. That’s not effective. This is best practice to work with templates. 6 00:00:43,640 --> 00:00:49,960 So, if we work in the team, the front end developer can work easily without touch the 7 00:00:49,960 --> 00:00:56,660 Django code, they can focus on html, css, and javascript code. 8 00:00:58,240 --> 00:01:06,720 Okay, I will show you how to use templates in Django. First, we need to define the directory 9 00:01:06,720 --> 00:01:13,720 where we will save our templates in settings.py, in the Templates section. 10 00:01:21,460 --> 00:01:27,840 Now let’s create templates directory. Please notice that templates directory must in the 11 00:01:27,840 --> 00:01:31,580 same level as our project and app directory. 12 00:01:40,900 --> 00:01:44,340 Okay now we have templates directory 13 00:01:44,600 --> 00:01:54,480 Now let’s modify views.py file. Here we will not return the html expressions anymore 14 00:01:54,700 --> 00:01:58,860 We will return render and the template files. 15 00:02:12,000 --> 00:02:15,000 Here we can see the hello_world function 16 00:02:15,000 --> 00:02:18,020 will return hello_world.html. 17 00:02:18,600 --> 00:02:23,980 Now let’s create hello_world.html inside templates directory. 18 00:02:30,340 --> 00:02:36,680 You can write a simple html file in here, in this case I just want to print Hello World. 19 00:02:45,880 --> 00:02:50,920 Now let’s go to browser, navigate to app1/hello_world. 20 00:02:57,240 --> 00:03:00,580 Here we can see that we get hello world. 21 00:03:00,840 --> 00:03:08,400 This display is generated by our template. To make sure, let’s add some html syntax 22 00:03:08,400 --> 00:03:14,020 in the template file. Let's say we want to add Welcome to Django. 23 00:03:23,540 --> 00:03:31,960 Back to browser, refresh, and here we can see that we get Hello world 24 00:03:31,960 --> 00:03:38,760 and Welcome to Django. Okay this is mean that our template file is work! 25 00:03:39,100 --> 00:03:47,280 Now let’s manipulate all of function inside views.py to use templates. We will use a.html 26 00:03:47,340 --> 00:04:02,480 for function A, and b.html for function B. 27 00:04:09,660 --> 00:04:14,640 Now let’s create a.html & b.html 28 00:04:21,800 --> 00:04:30,800 Let’s just copy and paste from hello_world.html to a.html & b.html. 29 00:04:31,560 --> 00:04:41,760 For b.html, I want to display This is B, and for a.html, I want display This is A. 30 00:04:46,120 --> 00:04:56,640 Let’s back to the browser. Let’s go to a, here we can see that we get This is A, 31 00:04:56,880 --> 00:05:04,300 let’s go to b, and here we can see that we get This is B. 32 00:05:04,300 --> 00:05:09,180 Congratuliation! Now we are able to use templates in Django. 33 00:05:09,180 --> 00:05:16,820 In the next video we are going to learn about context. Thank you for watching and see you on the next video.