1
00:00:00,260 --> 00:00:01,280
Hey, welcome back.

2
00:00:01,310 --> 00:00:06,230
In the previous video, we built the code for index.html.

3
00:00:06,260 --> 00:00:13,250
Now we want to go to Views.py, which is under job application.

4
00:00:13,250 --> 00:00:22,340
It is in Views.py where we return the index.html file and we do that through this function.

5
00:00:22,340 --> 00:00:31,520
And in this function we should now get the values of those widgets, meaning that whatever the user

6
00:00:31,610 --> 00:00:39,170
enters here like our did, we want to capture that string in here, that text, We capture it as a string.

7
00:00:39,170 --> 00:00:45,650
So if I bring here the flask form app, which we built previously in the course.

8
00:00:47,480 --> 00:00:49,160
You will see that here.

9
00:00:49,160 --> 00:00:57,200
We also have this index function which starts with if request dot method is equal to post, and then

10
00:00:57,200 --> 00:01:05,630
we get the values of each input widget and we store those in variables, right?

11
00:01:06,020 --> 00:01:09,890
So similarly in Django, let's go back to Django.

12
00:01:10,460 --> 00:01:16,850
Similarly here we want to check if request dot method is equal.

13
00:01:17,300 --> 00:01:24,080
So make sure to have two assignment operators so if that is equal to the post string.

14
00:01:25,310 --> 00:01:29,760
Because we have two types of HTTP requests.

15
00:01:29,780 --> 00:01:36,150
One is a get request when we press the enter key in our URL.

16
00:01:36,190 --> 00:01:36,480
Right.

17
00:01:36,500 --> 00:01:38,120
That's a post request.

18
00:01:38,150 --> 00:01:42,740
The site is not running because now we have some syntax error here.

19
00:01:42,740 --> 00:01:44,330
The code is not complete.

20
00:01:44,570 --> 00:01:51,320
But what I'm trying to say is that this is a get request, which means we are getting data in our browser.

21
00:01:51,320 --> 00:01:54,750
We are getting that form to be displayed in our browser.

22
00:01:54,770 --> 00:01:55,910
That is a get request.

23
00:01:55,910 --> 00:02:03,260
But then when we fill in the data and we press that submit button, which should be somewhere in here,

24
00:02:03,530 --> 00:02:06,110
that is a post request.

25
00:02:06,110 --> 00:02:13,490
So as a user, as a visitor of the website, when you press that button, you are doing a post request,

26
00:02:13,490 --> 00:02:17,950
you are posting data to the server to the Django app.

27
00:02:17,960 --> 00:02:20,480
So this is what we're doing here.

28
00:02:20,480 --> 00:02:27,150
We're checking if the user is pressing a button, if the user is making a submit request.

29
00:02:27,150 --> 00:02:37,260
In other words, if that's the case, then we create some variables such as first name is equal to form

30
00:02:37,260 --> 00:02:37,980
dot.

31
00:02:38,010 --> 00:02:39,240
What is form?

32
00:02:40,200 --> 00:02:44,520
Form is something we need to create actually.

33
00:02:45,300 --> 00:02:53,190
So just above this line you want to say form is equal to contact form.

34
00:02:54,310 --> 00:02:58,180
Which is a class that we will need to create.

35
00:02:58,390 --> 00:03:03,400
So in contrast to Flask, getting the data with Django is a bit different.

36
00:03:03,430 --> 00:03:08,140
We need to create a new class and we'll call this class Contactform.

37
00:03:08,140 --> 00:03:14,960
Now, this class should be created inside job application, right?

38
00:03:14,980 --> 00:03:24,180
So right click over job application, go to new and go to Python file and let's name this forms.

39
00:03:24,190 --> 00:03:29,890
So that will create a new forms.py file.

40
00:03:29,890 --> 00:03:38,890
So make sure that forms.py is in the same directory with views.py which is the job application directory.

41
00:03:38,920 --> 00:03:39,490
Right.

42
00:03:39,490 --> 00:03:52,330
And inside forms that file we need to import from django import forms and then class contact form forms

43
00:03:52,330 --> 00:03:57,800
dot form we inherit from the form class of the forms.

44
00:03:57,800 --> 00:04:01,310
Module forms is a module of Django.

45
00:04:02,090 --> 00:04:02,690
Right.

46
00:04:03,930 --> 00:04:08,820
And so we will create a class here and then import this class from views.

47
00:04:08,970 --> 00:04:22,200
So in views we say from forms, import contact form, or you can say application form to be more.

48
00:04:26,120 --> 00:04:27,570
Clear what this is.

49
00:04:27,590 --> 00:04:32,930
So let's change it to application form since we have this job application.

50
00:04:34,110 --> 00:04:34,550
Right?

51
00:04:34,550 --> 00:04:40,550
So class application form, we import that class in here and we call it in here.

52
00:04:40,550 --> 00:04:43,040
So let's define the class.

53
00:04:45,300 --> 00:04:54,420
First name is equal to forms that car field with a max length of 80.

54
00:04:54,780 --> 00:04:58,300
Wait, but didn't we actually write this already?

55
00:04:58,320 --> 00:05:01,320
So does this recall anything?

56
00:05:01,350 --> 00:05:02,460
Well, yes.

57
00:05:02,490 --> 00:05:08,460
In the Models.py file, we already wrote something like what?

58
00:05:08,460 --> 00:05:09,660
We're writing now.

59
00:05:09,690 --> 00:05:10,350
So.

60
00:05:10,350 --> 00:05:16,230
But this here in Models.py is the model for the database table.

61
00:05:16,230 --> 00:05:23,880
And this here is the model for the application form, which happened to be the same, Right?

62
00:05:23,910 --> 00:05:31,880
So what we should do here is we can just copy all these lines from the models file.

63
00:05:31,890 --> 00:05:43,200
So I'm in Models.py here, I copy these, I go to forms.py delete that and paste those lines in here.

64
00:05:44,790 --> 00:05:48,660
And then of course, you want to change models to forms.

65
00:05:49,710 --> 00:05:55,080
So the forms module also have these classes care fields.

66
00:05:55,080 --> 00:05:55,860
ET cetera.

67
00:06:00,700 --> 00:06:06,210
Now, there is also another way which would avoid having to write the code again.

68
00:06:06,220 --> 00:06:14,980
But this method has more pros because the process is more manual, but it gives you more control so

69
00:06:14,980 --> 00:06:19,360
that you can distinguish between the models and the forms.

70
00:06:19,360 --> 00:06:24,700
So if you want to change something in the forms, then this method makes it possible.

71
00:06:24,700 --> 00:06:28,870
So we do have duplicate code, but that's fine.

72
00:06:28,990 --> 00:06:32,520
And it's also good for you as a starter in Django.

73
00:06:32,530 --> 00:06:41,920
So once we have this application form class, so application form which inherits from form, then we

74
00:06:41,920 --> 00:06:53,320
import application form from within views, we import it, then we instantiate it here under if request

75
00:06:53,320 --> 00:07:00,650
method post, and then we use that instance form dot cleaned.

76
00:07:02,380 --> 00:07:11,980
Dot to square brackets first name so form dot clean data contains a dictionary.

77
00:07:11,980 --> 00:07:23,800
So that dictionary looks something like you know, first name has John let's say John input some data,

78
00:07:23,830 --> 00:07:31,600
then it has another last name Key Smith perhaps, and so on.

79
00:07:31,600 --> 00:07:38,890
So this dictionary is constructed by Django and stored in form dot clean data.

80
00:07:38,890 --> 00:07:45,070
So that will give us the value of the first name key for of that dictionary.

81
00:07:45,070 --> 00:07:52,210
So we do the same with Ctrl D or command D, you duplicate that and do last name.

82
00:07:53,500 --> 00:07:54,910
Last name.

83
00:07:55,860 --> 00:07:57,050
Duplicate it again.

84
00:07:57,060 --> 00:07:58,560
This will be email.

85
00:07:59,490 --> 00:08:00,210
Email.

86
00:08:00,210 --> 00:08:09,360
So these strings here, these correspond to index.html, the name of each widget.

87
00:08:09,360 --> 00:08:12,750
So we have the input widget for the first name.

88
00:08:12,930 --> 00:08:13,710
Right.

89
00:08:14,010 --> 00:08:20,370
And this has as name first name the other one has last name email date.

90
00:08:20,370 --> 00:08:27,510
So make sure that these names correspond to what we're writing in views Dot pi.

91
00:08:28,020 --> 00:08:30,720
So that is the date.

92
00:08:30,750 --> 00:08:31,800
The next one.

93
00:08:31,840 --> 00:08:32,460
Right?

94
00:08:32,490 --> 00:08:36,419
Then we have occupation.

95
00:08:36,539 --> 00:08:39,960
Occupation is occupation.

96
00:08:40,860 --> 00:08:43,620
So again, duplicate.

97
00:08:45,200 --> 00:08:46,400
Occupation.

98
00:08:48,760 --> 00:08:49,830
Occupation.

99
00:08:49,830 --> 00:08:57,900
So at this point, we should be able to get the users data inside these variables.

100
00:08:58,170 --> 00:09:03,560
But there is one more thing we need to add, and that is the form validation.

101
00:09:03,570 --> 00:09:12,300
You have to validate the form using an if conditional block here if form is underscore valid.

102
00:09:12,960 --> 00:09:24,060
So if that's the case, you want to execute all these, so select all these lines and press tab to indent

103
00:09:24,060 --> 00:09:26,280
them under the if condition.

104
00:09:26,970 --> 00:09:34,710
And then you also want to add here an argument for this class because this class inherits from.

105
00:09:35,630 --> 00:09:45,950
The form class and the form class expects an argument and that argument is request.post a post object.

106
00:09:45,950 --> 00:09:50,300
And yeah, with this we can go to the site, reload and see what we've got.

107
00:09:50,310 --> 00:09:52,460
So the app is not running.

108
00:09:52,460 --> 00:10:00,110
When you see this, there is an issue which doesn't let the app run at all and you'll see that error

109
00:10:00,110 --> 00:10:00,530
in here.

110
00:10:00,530 --> 00:10:01,610
Module not found error.

111
00:10:01,640 --> 00:10:05,690
No module named form in line two of views.py.

112
00:10:06,080 --> 00:10:06,590
Right.

113
00:10:06,590 --> 00:10:07,940
Which takes us here.

114
00:10:07,970 --> 00:10:09,110
Views.py.

115
00:10:09,140 --> 00:10:10,040
Line two.

116
00:10:10,070 --> 00:10:13,040
There is no module named forms.

117
00:10:13,550 --> 00:10:16,160
That's because we need to add a.here.

118
00:10:16,730 --> 00:10:23,150
Since forms is a local file we have in this directory.

119
00:10:23,150 --> 00:10:25,100
So job application.

120
00:10:25,100 --> 00:10:29,600
So to be able to import that local file you should use that dot.

121
00:10:29,630 --> 00:10:38,430
Otherwise Django is trying to import something from the root directory which is app 17 django form,

122
00:10:38,430 --> 00:10:45,030
but we want to import forms that py which is inside this directory job application.

123
00:10:45,030 --> 00:10:49,500
So the directory where views is also located.

124
00:10:49,500 --> 00:10:51,810
In that case we use dot form.

125
00:10:51,810 --> 00:10:56,700
So dot forms import application form and then we go to reload the page again.

126
00:10:56,700 --> 00:10:58,170
And now we see the form.

127
00:10:58,170 --> 00:11:06,930
So let's fill in some data, but let's actually print out just to see if we got the first name correctly,

128
00:11:06,930 --> 00:11:07,890
for example.

129
00:11:07,890 --> 00:11:10,740
So just first name and reload.

130
00:11:10,740 --> 00:11:17,310
First reload so that it reads the new change and then filling the data.

131
00:11:20,070 --> 00:11:21,270
Press submit.

132
00:11:21,480 --> 00:11:24,790
Go to PyCharm and search here.

133
00:11:24,810 --> 00:11:27,720
Look for the name you entered.

134
00:11:27,720 --> 00:11:29,250
So in my case.

135
00:11:29,250 --> 00:11:31,820
So this was printed out by this line.

136
00:11:31,830 --> 00:11:35,670
That means we were able to fetch the data.

137
00:11:35,700 --> 00:11:40,860
The user enters successfully and store them inside the variables.

138
00:11:40,980 --> 00:11:44,810
So let's close this video at this point.

139
00:11:44,820 --> 00:11:55,380
So as a wrap up, what we just did in this video is we created an application form class inside forms.py,

140
00:11:56,130 --> 00:11:58,050
and then we import it.

141
00:11:58,050 --> 00:12:06,840
That application form class, we instantiated it here, we validate it here, and then we extract the

142
00:12:06,840 --> 00:12:16,710
user values in these lines out of that form instance using the cleaned data dictionary.

143
00:12:16,830 --> 00:12:20,020
And then we store all of these in variables.

144
00:12:20,020 --> 00:12:25,660
And now next step is to store these values in the database.

145
00:12:25,690 --> 00:12:26,980
Let's do that in the next video.

146
00:12:27,010 --> 00:12:27,520
See you there.

