1
00:00:05,290 --> 00:00:06,360
Welcome back.

2
00:00:06,400 --> 00:00:08,960
In part two of our databases views lecture.

3
00:00:09,070 --> 00:00:13,210
We're going to continue right where we left off last time by adding in the views and the routes to the

4
00:00:13,210 --> 00:00:14,140
templates.

5
00:00:14,140 --> 00:00:16,630
Let's head back to our text editor and continue.

6
00:00:16,630 --> 00:00:19,190
All right here we are back at the actual text editor.

7
00:00:19,330 --> 00:00:21,000
We just create our Papy model.

8
00:00:21,070 --> 00:00:26,860
Now we're going to do is after this entire adoption site thought py file I'm going to create the actual

9
00:00:26,860 --> 00:00:33,180
view functions in this view functions are going to have forms with them.

10
00:00:33,800 --> 00:00:39,190
So that's going to allow us to actually take in user information and then save it to the models.

11
00:00:39,500 --> 00:00:40,920
So we're gonna have quite a few views.

12
00:00:40,940 --> 00:00:43,070
The first one is the home page.

13
00:00:43,430 --> 00:00:49,700
So say app that route and it's just going to go straight to the home page.

14
00:00:50,240 --> 00:01:01,380
Will say the death index and we're going to render or return rendered template and we're just going

15
00:01:01,380 --> 00:01:03,390
to connect it to home.

16
00:01:03,420 --> 00:01:06,130
H T.M..

17
00:01:06,330 --> 00:01:11,350
And let me collapse this left hand sidebar so we have our home page and in the next couple of viewers

18
00:01:11,350 --> 00:01:16,060
who wants is going to be a view for adding a puppy and another view for listing all the puppies from

19
00:01:16,060 --> 00:01:16,800
the database.

20
00:01:17,020 --> 00:01:25,170
And another view to remove a puppy or say app route and let's make the view for adding a puppy will

21
00:01:25,230 --> 00:01:30,240
say forward slash add and you can technically call that whatever you want as far as your L extension

22
00:01:31,110 --> 00:01:39,450
then we'll say methods and also the methods equal to get and then post.

23
00:01:39,680 --> 00:01:43,800
And then here after this gets couple more lines here we'll do the following.

24
00:01:43,910 --> 00:01:53,910
We'll say DMF and underscore pup we'll create a form and this form is going to be an added form which

25
00:01:53,910 --> 00:01:57,690
means we should go to our forms that PI file and create the forms.

26
00:01:57,930 --> 00:01:59,970
So we have an ad form that we're going to need.

27
00:01:59,970 --> 00:02:04,830
So let's take a quick break and then come over here to forms that pi and actually create that form.

28
00:02:04,830 --> 00:02:09,960
Because if you remember up here from the top we said from forms import add form and delete form.

29
00:02:10,020 --> 00:02:15,270
So we should probably now create those cycloid to uncomment that out come to forms that pi and then

30
00:02:15,270 --> 00:02:16,980
fill this out with those forms.

31
00:02:16,980 --> 00:02:20,950
This is actually pretty straightforward it's the same thing we've done in the forms section of the course.

32
00:02:21,330 --> 00:02:33,440
We'll say from flask underscored WITF import flask form and then from W.T. forms import and the fields

33
00:02:33,440 --> 00:02:41,370
we're going to be using is a string field in integer field and they submit field that here will say

34
00:02:41,370 --> 00:02:52,430
class add a form will inherit from flask form and or senator is going to have a name option to add the

35
00:02:52,430 --> 00:03:00,670
name of the puppy or say String Field name of puppy and then we'll just have them submit the actual

36
00:03:00,670 --> 00:03:06,420
puppy names so there will be a submit field it'll just be at puppy.

37
00:03:06,470 --> 00:03:10,320
So that's the ad form and a little bit we'll connect it to a template.

38
00:03:10,790 --> 00:03:14,670
And then the other one or the other class we're going to do is a delete form.

39
00:03:14,750 --> 00:03:25,220
So say the form and this inherits from flask form we'll say ID is equal to integer field and this will

40
00:03:25,220 --> 00:03:32,060
be the id number of puppy to remove.

41
00:03:32,060 --> 00:03:35,930
So we're going to have the user instead of passing in the name of the puppy they want to remove because

42
00:03:35,930 --> 00:03:37,550
puppies could share the same name.

43
00:03:37,580 --> 00:03:42,540
Instead they'll provide the ID number for the puppy they want to remove from the database.

44
00:03:42,710 --> 00:03:51,550
They will say submit is equal to I will say submit field remove.

45
00:03:51,570 --> 00:03:56,670
OK so now that we have our add a form or delete form created we can come back to adoption site and then

46
00:03:56,670 --> 00:03:59,860
begin to use those forms inside of these view functions.

47
00:03:59,910 --> 00:04:02,280
So we set up our home page view that's no problem.

48
00:04:02,280 --> 00:04:07,330
And now with add up we can get that out of form working.

49
00:04:07,380 --> 00:04:13,930
So what we're going to do after we have the ad form will create an instance of it and then offer that

50
00:04:14,110 --> 00:04:16,150
will say add a new puppy to the database.

51
00:04:16,150 --> 00:04:25,920
So if that form validate on submit then we're going to grab the name that they provided for the puppy

52
00:04:26,340 --> 00:04:28,330
from the form name.

53
00:04:28,530 --> 00:04:31,950
Data and then we're going to add a new puppy to the database.

54
00:04:31,970 --> 00:04:38,340
So I'll say that will say new pup is equal to a puppy object name.

55
00:04:38,360 --> 00:04:39,820
Remember this puppy right here.

56
00:04:39,950 --> 00:04:44,480
That's just a model we created up here in a much larger scope.

57
00:04:44,480 --> 00:04:49,940
You'll have a separate models up profile and then say from models import puppy and then it's very similar

58
00:04:49,940 --> 00:04:52,760
to the way that it forms here will say puppy.

59
00:04:52,760 --> 00:05:04,420
Take that name then we'll say D-B session and we'll add a new pup then we'll say D-B session and we'll

60
00:05:04,420 --> 00:05:12,550
commit those changes they'll say return and we'll redirect to your L for.

61
00:05:12,880 --> 00:05:15,280
And we're going to make of you for listing all the puppies.

62
00:05:15,280 --> 00:05:16,860
It's all a list pup.

63
00:05:17,020 --> 00:05:23,140
We haven't created this puppy yet but we will that's if the form is valid on Simitian the default view

64
00:05:23,140 --> 00:05:25,480
for at pup should go to the page.

65
00:05:25,570 --> 00:05:36,070
So we'll say return rendered template ad T.M. and then we pass in the form as form.

66
00:05:36,080 --> 00:05:39,570
All right so if we zoom out a little bit here we can see the whole view.

67
00:05:39,680 --> 00:05:45,620
I have this add put view I create a version of the ADD form which remembers just this form appear in

68
00:05:45,620 --> 00:05:47,750
the forms that py file.

69
00:05:47,750 --> 00:05:50,900
Then I say it's a form as validation is valid on submission.

70
00:05:50,930 --> 00:05:54,820
Then grabbed the name that they provided create a new puppy with that name.

71
00:05:54,860 --> 00:05:59,990
Add that puppy to the database and commit and save those changes and then redirect them to a list of

72
00:05:59,990 --> 00:06:01,990
all the puppies.

73
00:06:02,000 --> 00:06:03,600
Let's go ahead and create that list pup.

74
00:06:03,620 --> 00:06:06,440
You know it's going to zoom back in a little bit.

75
00:06:06,620 --> 00:06:08,170
Come back down here.

76
00:06:08,310 --> 00:06:20,740
It's the following will say app route will say forward slash list and then TGF list underscore pup.

77
00:06:20,940 --> 00:06:24,930
And we're going to grab a list of puppies from the database so we'll do the following.

78
00:06:24,960 --> 00:06:34,250
We'll say puppies is equal to puppy dog queery the all.

79
00:06:34,250 --> 00:06:38,810
And then we will return render template list.

80
00:06:38,830 --> 00:06:44,860
H T.M. where puppies is equal to puppies.

81
00:06:44,980 --> 00:06:45,860
Save that.

82
00:06:46,190 --> 00:06:51,080
So we have our list pup and then we're going to grab all the puppies in the database and then just pass

83
00:06:51,080 --> 00:06:56,140
them on to this list HTL template now that we have that going.

84
00:06:56,160 --> 00:06:59,550
Let's go ahead and create a view to remove a puppy.

85
00:06:59,770 --> 00:07:12,630
So I'll say this at the forward slash deletes the methods here are going to be get and post because

86
00:07:12,630 --> 00:07:21,180
now we're going to have a delete form or say this delete pup view create a form that's delete form that

87
00:07:21,180 --> 00:07:21,760
we created.

88
00:07:21,780 --> 00:07:29,040
So create an instance of that form and if the form is valid when they hit submit or we end up doing

89
00:07:29,160 --> 00:07:36,840
is we say Id equal to form the ID data because remember if we come back here to forms that pi we just

90
00:07:36,840 --> 00:07:39,310
pasand the idea of the puppy that we want to remove.

91
00:07:39,510 --> 00:07:49,220
So he grabbed that from that ID and then will create a puppy say puppy dog queery get and then will

92
00:07:49,220 --> 00:07:55,140
say id and we dont need to say that first or all because the ID is a primary key.

93
00:07:55,220 --> 00:07:58,890
So the ID should only uniquely identify one puppy to remove.

94
00:07:59,240 --> 00:08:08,190
Once we get that puppy we're going to say D-B session delete and then we're going to say pup will commit

95
00:08:08,190 --> 00:08:09,250
those changes.

96
00:08:11,350 --> 00:08:23,540
All right after that we're just going to return redirect your L4 and this case will say list up.

97
00:08:23,770 --> 00:08:29,050
And then for the overall delete puppy page we're going to render the template for delete that each team

98
00:08:29,060 --> 00:08:39,450
T.M. and then we say form Ezekial to form and then finally we'll say if name is equal to mean take the

99
00:08:39,450 --> 00:08:44,730
app and run it and we'll say de-bug is equal to true.

100
00:08:44,890 --> 00:08:49,670
And now you've completed everything that we need to do inside of our adoption site that py file.

101
00:08:49,690 --> 00:08:51,310
So let's do a quick review of all this.

102
00:08:51,340 --> 00:08:56,470
And the next lecture will fill out all the HTML templates and then we'll help debug anything that may

103
00:08:56,470 --> 00:08:58,410
have gone wrong or may have been missing.

104
00:08:58,680 --> 00:09:02,840
But I'm going to zoom out just looking at the whole picture of everything we've done so far.

105
00:09:03,060 --> 00:09:08,970
We've done our imports set up our flask application set up the configuration seeker key for forms set

106
00:09:08,970 --> 00:09:15,600
up the configurations for a sequel database set up the database then the migrations create a model for

107
00:09:15,600 --> 00:09:16,830
that database.

108
00:09:16,980 --> 00:09:19,040
Then we have two functions for the forms.

109
00:09:19,110 --> 00:09:25,080
Our home page the puppy page and then we actually create an instance of a form which you remember we

110
00:09:25,080 --> 00:09:29,120
created to form here in the forms up-I file in add form and the leaf form.

111
00:09:29,250 --> 00:09:36,190
Just a way to grab a name and an ID from a user and then from there we have a list puppie view which

112
00:09:36,190 --> 00:09:41,350
grabs all the puppies in our database and then we also have the delete view which grabs the ID that

113
00:09:41,350 --> 00:09:44,590
the user submits in the form and then delete some from the session.

114
00:09:44,620 --> 00:09:48,510
And that's the main idea of connecting templates forms and your database.

115
00:09:48,550 --> 00:09:53,050
Since you do you create an instance of a form and then use the information that the user provided.

116
00:09:53,170 --> 00:09:58,300
Queery your database somehow and then interact with it through a session and then redirect them to some

117
00:09:58,300 --> 00:09:59,040
other page.

118
00:09:59,170 --> 00:10:01,300
That's the main concept that we need to really understand.

119
00:10:01,450 --> 00:10:03,610
For this particular lecture.

120
00:10:03,610 --> 00:10:04,110
All right.

121
00:10:04,370 --> 00:10:09,760
Once we done that we can then begin working on connecting all these views to their prospective html

122
00:10:09,760 --> 00:10:10,460
files.

123
00:10:10,510 --> 00:10:15,640
So we have base 30 smell that we still need to fill out the home page the ad page daily HTL and then

124
00:10:15,640 --> 00:10:19,870
list h HTML so will be filling out all those template files in the next lecture.

125
00:10:19,870 --> 00:10:20,580
We'll see if.
