1 00:00:01,140 --> 00:00:07,660 This is the first video in this course that will teach you about Django. In this video, 2 00:00:07,660 --> 00:00:12,280 we are going to create a very simple Django project to display Hello World 3 00:00:13,020 --> 00:00:24,480 Okay Let’s start, open a commend prompt and create a new project by typing Django-admin 4 00:00:24,480 --> 00:00:32,360 startproject and the name of the project. In this case, the name of my project is first_project. 5 00:00:38,360 --> 00:00:44,700 After create a new project, Django will create a directory with the same name as our project. 6 00:00:45,260 --> 00:00:55,700 Let’s try to dir, here we can see that we have first_project directory. Let’s cd to 7 00:00:55,740 --> 00:00:56,940 this directory 8 00:00:58,020 --> 00:01:05,340 Next, we need to create a Django app by typing python manage.py and the name of app, in this 9 00:01:05,340 --> 00:01:08,560 case, the name of my app is app1 10 00:01:12,680 --> 00:01:19,140 When we create an app in Django, Django will also automatically create a directory with 11 00:01:19,140 --> 00:01:28,780 the same name as the app for us. Let’s try to dir, here we can see that we have app1 directory 12 00:01:29,720 --> 00:01:37,400 Now let’s open our code editor. If you use visual studio code, you can simply type code 13 00:01:37,400 --> 00:01:42,440 space dot and the visual studio code will open in the current directory. 14 00:01:48,600 --> 00:01:54,900 Here we can see our first_project. You can see that we have a lot of file in here 15 00:01:55,240 --> 00:01:59,600 All of this file is automatically generated by Django 16 00:02:00,340 --> 00:02:09,480 Let’s try to run our project. Open the command prompt and type python manage.py runserver. 17 00:02:13,980 --> 00:02:21,300 We will see warning that you have some unplied migrations like this, but for now, we can 18 00:02:21,300 --> 00:02:26,080 ignore it, we will discuss about that later in the next video 19 00:02:26,620 --> 00:02:36,340 Here we can see that our server is running on 127.0.0.1:8000. Let’s open our browser 20 00:02:38,480 --> 00:02:43,580 and go to 127.0.0.1:8000. 21 00:02:50,120 --> 00:02:53,740 Here we get the default display of Django 22 00:02:54,180 --> 00:02:58,180 this is mean that our project is running successfully. 23 00:02:58,400 --> 00:03:06,080 Now let’s try to manimpulate our project to display hello world. Open a visual studio code. 24 00:03:06,160 --> 00:03:12,820 First thing that we have to do is register our new app, which is app1. We can register 25 00:03:12,820 --> 00:03:27,520 it on settings.py. Here in the INSTALLED_APPS, let’s add our new app. 26 00:03:31,380 --> 00:03:39,740 Next let’s edit urls.py file, here we can remove the comment so we can read the important 27 00:03:39,740 --> 00:03:55,700 script easily. Let’s add a new path here, we will use root url here, and include app1.urls, 28 00:03:59,780 --> 00:04:06,160 means that the root url will read for urls.py file inside app1. 29 00:04:06,860 --> 00:04:17,200 Next, if we see here, inside app1 directory, we don’t have urls.py file, so we need to created it first. 30 00:04:24,400 --> 00:04:31,420 We can just copy paste the urls script from the urls.py file inside first_project directory 31 00:04:34,860 --> 00:04:42,740 Oh ya, if we see here, we use an include keyword, so we need to import it first. 32 00:04:47,600 --> 00:04:56,400 In the urls.py inside app1 directory, we don’t need admin, so we can delete it. We also need 33 00:04:56,400 --> 00:04:58,540 to delete this line. 34 00:05:03,700 --> 00:05:12,360 Here, I will create a new path hello_world. Here we can replace the include with views.hello_world. 35 00:05:17,740 --> 00:05:24,480 This script mean, if we go to hello_world url, we will redirected to the hello_world 36 00:05:24,480 --> 00:05:27,380 function inside views.py 37 00:05:28,540 --> 00:05:35,820 Here we can see that we have views.py file, later we will create hello_world function 38 00:05:35,820 --> 00:05:43,580 inside this file. But before that, we need to import views In here, because we use it here 39 00:05:50,640 --> 00:05:55,040 Now let’s create hello_world function in views.py, 40 00:06:08,120 --> 00:06:11,440 here we need to add an argument request 41 00:06:11,920 --> 00:06:20,520 in this video, we will just return simple html expressions, so we need to import httpresponse 42 00:06:20,520 --> 00:06:34,720 from Django.shortcuts. Next, inside hello_world function, we need to return a simple html 43 00:06:34,720 --> 00:06:42,600 tag using httpresponse, in this case I will just return “Selamat datang di Django” 44 00:06:42,600 --> 00:06:49,540 with H1 tag. Selamat datang di Django is “Welcome to Django” in Indonesian. 45 00:06:54,100 --> 00:07:01,100 Okay we have done here, let’s take a look at command prompt, make sure that we don’t 46 00:07:01,220 --> 00:07:09,440 have any error. Okay we don’t have any error here. Let’s go to our browser and navigate 47 00:07:09,440 --> 00:07:22,520 to 127.0.0.1:8000/hello_world. Here we can see that we get “Selamat Datang di Django”. 48 00:07:24,420 --> 00:07:33,320 Now let’s try to remove hello_world from url and see what happened. We will see 404 eror here 49 00:07:33,380 --> 00:07:41,080 Why? let’s go back to visual studio code and open urls.py inside first_project, 50 00:07:41,080 --> 00:07:50,580 here we can see that the root url is point to urls.py inside app1, and if we see the 51 00:07:50,580 --> 00:08:01,860 urls.py inside app1, here we don’t have a root url, we just have hello_world url. 52 00:08:02,180 --> 00:08:08,780 That’s why if we remove hello_world from our browser, we will get 404 error. 53 00:08:09,380 --> 00:08:13,500 Now let's try to remove hello_world url here. 54 00:08:17,580 --> 00:08:26,580 Back to browser and refresh. Okey, now we can see “Selamat datang di Django” in 55 00:08:26,580 --> 00:08:34,980 root url. But if we try to put hello_world again in the url, we will absolutely get 404 56 00:08:34,980 --> 00:08:39,880 error, that is because we don’t have hello_world url. 57 00:08:40,240 --> 00:08:45,520 May be you will be little confuse about the url, but don’t worry, we will cover about 58 00:08:45,520 --> 00:08:48,180 url deeply in the next video. 59 00:08:48,920 --> 00:08:54,960 I hope you understand about this basic concept of Django. I recommend you to re watch this 60 00:08:54,960 --> 00:08:59,960 video if you don’t really understand about the basic concept of Django. 61 00:09:00,200 --> 00:09:08,340 And if you have any further questions, feel free to ask me and I will do my best to answer all of your question 62 00:09:08,620 --> 00:09:11,580 Thankyou for watching and See you on the next video.