1
00:00:00,230 --> 00:00:01,200
Hey, welcome back.

2
00:00:01,220 --> 00:00:04,180
In the previous video, we worked on the database model.

3
00:00:04,190 --> 00:00:12,890
So if we run the program, the app now with Python Manage.py run server.

4
00:00:14,000 --> 00:00:17,720
We should see this on our browser.

5
00:00:17,750 --> 00:00:19,640
Now this is the home page.

6
00:00:19,640 --> 00:00:23,990
So how can we display a custom home page here?

7
00:00:24,020 --> 00:00:30,860
Well, we can do that using view functions and HTML view functions are.

8
00:00:31,850 --> 00:00:36,770
Let me bring you the Flask app we developed in the previous sections.

9
00:00:37,250 --> 00:00:43,250
So in our Flask app index, this index function was a view function.

10
00:00:43,250 --> 00:00:49,370
So the index function was executed when the home page was visited.

11
00:00:49,370 --> 00:00:50,140
Right?

12
00:00:50,150 --> 00:00:59,420
And so the index function that then returns the index.html file so that the user can see the website

13
00:00:59,450 --> 00:01:01,030
on their browser.

14
00:01:01,040 --> 00:01:08,510
So Django view functions are similar to define a Django view function.

15
00:01:08,510 --> 00:01:16,830
You want to locate your app directory job application in our case and then locate the views.py file

16
00:01:16,830 --> 00:01:24,780
which was generated in the beginning when we executed the command python manage.py start app.

17
00:01:24,780 --> 00:01:33,450
So you want to delete that comment and start creating your views with define followed by the name of

18
00:01:33,450 --> 00:01:37,170
the function followed by a request argument.

19
00:01:37,170 --> 00:01:43,020
So that's a parameter that the function expects from Django.

20
00:01:43,020 --> 00:01:49,290
So when Django calls it Django passes a request on the background so you don't have to worry to call

21
00:01:49,290 --> 00:01:50,400
this function right.

22
00:01:50,430 --> 00:01:58,050
The function will be called when the user visits the URL here, and then the function will return an

23
00:01:58,350 --> 00:02:00,030
HTML template.

24
00:02:00,060 --> 00:02:04,770
Now with Flask, we use the render template function.

25
00:02:04,770 --> 00:02:06,960
With Django, it's just render.

26
00:02:07,320 --> 00:02:12,900
So make sure that render is imported here from Django dot shortcuts import render.

27
00:02:12,900 --> 00:02:22,370
And this expects now the name of the HTML file which we haven't created yet to create this file.

28
00:02:22,460 --> 00:02:23,990
Let me show you something first.

29
00:02:23,990 --> 00:02:35,840
If you go to settings in your Mysite directory settings dot pi and you should be able to see the templates

30
00:02:35,870 --> 00:02:39,650
variable here which defines a this key.

31
00:02:39,650 --> 00:02:47,300
So it's a list of dictionaries and this dictionary has this dirs and here you can specify directory

32
00:02:47,300 --> 00:02:55,460
names where you want to place your HTML files, but if you don't specify them, which I do recommend

33
00:02:55,460 --> 00:02:58,340
to do so don't specify any names, just close this.

34
00:02:58,370 --> 00:02:59,840
Don't change anything.

35
00:02:59,930 --> 00:03:10,220
So if you don't specify them, then Django assumes that they are inside a template directory which should

36
00:03:10,220 --> 00:03:13,550
be located inside your job application folder.

37
00:03:13,550 --> 00:03:15,840
So inside your app.

38
00:03:15,840 --> 00:03:22,620
So job application templates and inside templates you want to create an HTML file.

39
00:03:22,620 --> 00:03:28,980
So go to html file, name this index.html and don't delete this.

40
00:03:29,790 --> 00:03:30,890
I'm adding this to git.

41
00:03:30,900 --> 00:03:31,920
I'm using git.

42
00:03:32,010 --> 00:03:33,540
Don't delete these lines.

43
00:03:33,540 --> 00:03:36,180
We will use this as a template for now.

44
00:03:36,180 --> 00:03:42,960
So I want to show you a generic Django app first and then we create that job application form.

45
00:03:42,960 --> 00:03:50,250
So in the body here I'll just write hello, or maybe use some H1 tags to wrap this around.

46
00:03:51,880 --> 00:03:52,700
Like that.

47
00:03:52,720 --> 00:03:55,580
So this is what we've got, right?

48
00:03:55,600 --> 00:04:02,770
An index function which returns the rendered index.html template.

49
00:04:02,800 --> 00:04:04,390
That's good so far.

50
00:04:04,490 --> 00:04:10,210
Our boats, we don't have a decorator here like we had with the flask app.

51
00:04:10,210 --> 00:04:10,750
Right?

52
00:04:10,750 --> 00:04:12,400
With the Flask app.

53
00:04:12,520 --> 00:04:14,200
We had this.

54
00:04:16,430 --> 00:04:19,779
Decorator which attached this URL.

55
00:04:19,820 --> 00:04:21,800
The home page to this function.

56
00:04:21,950 --> 00:04:25,040
But in Django it doesn't work like that.

57
00:04:25,520 --> 00:04:30,350
In Django, there's another way to connect a function.

58
00:04:30,380 --> 00:04:35,750
A view function to a URL so that when the user visits that URL.

59
00:04:35,780 --> 00:04:38,120
This function is executed.

60
00:04:38,150 --> 00:04:39,920
To connect a function.

61
00:04:39,950 --> 00:04:46,430
A view function to a URL, you should create another job application.

62
00:04:46,430 --> 00:04:52,190
You should create a new urls.py file.

63
00:04:52,320 --> 00:04:53,540
Add it to git.

64
00:04:53,570 --> 00:05:05,570
If you're using git inside here you want to import from django dot urls import path and also from dot

65
00:05:05,600 --> 00:05:08,450
import views.

66
00:05:08,600 --> 00:05:12,560
So this is something from django from the library.

67
00:05:12,590 --> 00:05:13,220
Right.

68
00:05:13,220 --> 00:05:17,390
And this here is the views dot py file.

69
00:05:17,400 --> 00:05:21,360
We are importing this file as a module inside this file.

70
00:05:21,360 --> 00:05:29,370
So that works because views and urls.py are in the same directory under job application.

71
00:05:29,370 --> 00:05:30,110
Right.

72
00:05:30,120 --> 00:05:36,990
So then you want to define a url patterns variable.

73
00:05:37,590 --> 00:05:42,810
Be careful here because you should use the exact name as you see here.

74
00:05:42,810 --> 00:05:50,310
So URL patterns without spaces without underscores because Django expects will look for this variable

75
00:05:50,310 --> 00:05:51,030
name here.

76
00:05:51,030 --> 00:05:52,650
So then this will be a list.

77
00:05:52,650 --> 00:05:55,620
I'll just split that to have multiple lines.

78
00:05:55,830 --> 00:06:01,460
And then you call that path function, which takes three arguments.

79
00:06:01,470 --> 00:06:05,940
This one would be like the subdirectory in the URL.

80
00:06:05,970 --> 00:06:09,150
Perhaps you want it like an about page or and so on.

81
00:06:09,750 --> 00:06:17,070
But since this is the home page, you just leave it an empty string and then you do views dot index.

82
00:06:17,070 --> 00:06:22,230
So we imported views to this file and this file has an index function.

83
00:06:22,230 --> 00:06:30,000
So basically we are saying here that the home page should be connected to this function and then you

84
00:06:30,000 --> 00:06:33,840
want to give a name to this which will be used later by Django.

85
00:06:34,740 --> 00:06:39,720
So you should give the same name as this function name, but as a string.

86
00:06:39,720 --> 00:06:41,130
So that's a string.

87
00:06:41,130 --> 00:06:43,050
That's not a string.

88
00:06:43,050 --> 00:06:43,770
Right?

89
00:06:44,070 --> 00:06:46,320
You should be able to tell the difference here.

90
00:06:46,320 --> 00:06:50,280
So that's a list with one item for now.

91
00:06:50,280 --> 00:06:54,150
So the item is the call of the path function.

92
00:06:54,180 --> 00:06:58,980
Then there's one more thing we have to do regarding the URLs.

93
00:06:59,460 --> 00:07:08,430
You want to collapse this to close this directory and then open the mysite directory and inside my sites

94
00:07:08,430 --> 00:07:12,000
you will find a urls.py file.

95
00:07:12,000 --> 00:07:17,250
So this was generated again by Django when we set up the project.

96
00:07:17,280 --> 00:07:20,370
Now these are the URLs of the project.

97
00:07:20,370 --> 00:07:21,000
Right?

98
00:07:21,030 --> 00:07:26,850
The other URLs that py file contains the URL patterns of the app.

99
00:07:26,850 --> 00:07:32,040
So our job application app could have one or more URLs.

100
00:07:32,040 --> 00:07:32,610
Right.

101
00:07:32,610 --> 00:07:34,020
And then in here.

102
00:07:34,020 --> 00:07:43,650
So in this URL patterns list you want to go after the comma press enter and add a new URL pattern.

103
00:07:43,650 --> 00:07:46,320
So this is again an empty string.

104
00:07:46,320 --> 00:07:56,610
And then you say include and then you specify the name of your app, which is job underscore application

105
00:07:56,820 --> 00:07:59,100
dot URLs.

106
00:07:59,100 --> 00:08:08,520
So this comes as a string and it points to the urls.py file of the job application app.

107
00:08:08,790 --> 00:08:18,510
So that URLs file which contains the patterns of the app and include is something we need to import.

108
00:08:19,890 --> 00:08:21,390
So just go here.

109
00:08:21,420 --> 00:08:26,580
Django dot urls import path comma include.

110
00:08:27,420 --> 00:08:36,750
With that, we can go to this page reload and sometimes you might get errors and the errors will display

111
00:08:36,750 --> 00:08:37,970
something like this.

112
00:08:38,010 --> 00:08:45,990
To troubleshoot these errors, you should look at the exception location here, which tells you where

113
00:08:45,990 --> 00:08:48,810
in what file the error is occurring.

114
00:08:48,810 --> 00:08:56,910
So in my case it's views dot y line five index index is the name of the function.

115
00:08:57,030 --> 00:08:59,790
So let's go to that line.

116
00:09:00,820 --> 00:09:01,670
Views.

117
00:09:01,840 --> 00:09:03,060
Line five.

118
00:09:03,070 --> 00:09:11,140
So again, it's complaining that render is missing one required positional argument template name.

119
00:09:11,650 --> 00:09:15,280
So render actually gets more than one argument.

120
00:09:15,310 --> 00:09:20,320
So the first argument should be request this parameter.

121
00:09:20,320 --> 00:09:26,590
So that parameter goes as the first argument of this call the render call.

122
00:09:26,590 --> 00:09:27,100
Right.

123
00:09:27,310 --> 00:09:37,210
So this error Django thinks that you specified index.html as the request parameter and then you didn't

124
00:09:37,210 --> 00:09:38,860
specify the second parameter.

125
00:09:38,860 --> 00:09:43,690
So it thinks that the template name is missing as a second parameter.

126
00:09:43,690 --> 00:09:48,370
So please change that to this request and index.html.

127
00:09:48,670 --> 00:09:54,250
Then go back to the page reload and this is what you should see.

128
00:09:54,250 --> 00:09:56,230
If everything is working fine.

129
00:09:56,230 --> 00:10:04,250
So that is the hello H1 heading, which we served through our index.html file.

130
00:10:06,180 --> 00:10:10,650
So that's how you create a web page, a static web page with Django.

131
00:10:11,040 --> 00:10:19,110
Next in line is we want to add the form here and then accept user data in the form, store those data

132
00:10:19,110 --> 00:10:26,850
in the database, send an email to the user, and then finally create an admin interface to view the

133
00:10:26,880 --> 00:10:30,180
data that has been submitted from the user.

134
00:10:30,180 --> 00:10:32,670
So let's do that in the next video.

135
00:10:32,700 --> 00:10:33,600
Thanks a lot for this.

136
00:10:33,630 --> 00:10:34,080
See you later.

