1
00:00:05,320 --> 00:00:10,840
Welcome back in this lecture we're going to connect our databases to views.

2
00:00:11,000 --> 00:00:15,890
We finally learned enough to make a real web site with the key features of being able to display templates

3
00:00:15,980 --> 00:00:20,980
except user information reform's save supplied information in a database and then report back that saved

4
00:00:20,980 --> 00:00:23,120
the information from that database.

5
00:00:24,330 --> 00:00:28,350
We'll be creating an adoption site with some templates and we're going to have the ability to add new

6
00:00:28,350 --> 00:00:34,890
puppies list puppies and puppies from the database all by directly interacting of forms from templates.

7
00:00:34,920 --> 00:00:35,800
So let's get started.

8
00:00:35,820 --> 00:00:37,460
This is a big lecture for us.

9
00:00:37,470 --> 00:00:42,180
We're essentially connecting everything we've talked about already into one single web site project.

10
00:00:42,180 --> 00:00:44,130
So let's head over toward sex ed..

11
00:00:44,130 --> 00:00:44,360
All right.

12
00:00:44,360 --> 00:00:46,220
Here we are at the text editor.

13
00:00:46,260 --> 00:00:50,680
I'm going to start completely from scratch so you can get a feel for how it would be to start a Web

14
00:00:50,680 --> 00:00:52,360
site from the very beginning.

15
00:00:52,410 --> 00:00:58,310
The first thing I want to do is create two up-I scripts so don't underneath this folder flask examples.

16
00:00:58,350 --> 00:01:00,330
I'm going to say create a new file.

17
00:01:00,420 --> 00:01:05,720
The first one I'll just call it adoption underscore site dot pi.

18
00:01:05,940 --> 00:01:13,070
So again this adoption slimly mix has a comment adoption underscore site that pie.

19
00:01:13,240 --> 00:01:17,560
So I have that one pie script and since we're going to be creating forms I'm actually going to have

20
00:01:17,560 --> 00:01:23,640
those be in a separate file so I'll create a forms that py file and this will contain my forms.

21
00:01:23,710 --> 00:01:28,840
So this forms that file that's where I'm going to have my form classes instead of having everything

22
00:01:29,170 --> 00:01:31,280
in one large dot PI script.

23
00:01:31,540 --> 00:01:39,710
Then we're going to create a Templates folder say new folder templates and then inside of this we're

24
00:01:39,710 --> 00:01:41,510
going to have several templates.

25
00:01:41,510 --> 00:01:48,760
The first one that we pretty much always have is a base template so a new file based on HTL.

26
00:01:48,990 --> 00:01:51,850
And then once we have that we're going to create a couple of new files.

27
00:01:51,870 --> 00:01:57,320
Let's create one for our home page so we can call this either home that each female or index or a smell

28
00:01:57,330 --> 00:01:58,460
whatever you prefer.

29
00:01:58,710 --> 00:02:03,840
And then we're going to have views to add puppies list the puppies and delete the puppies.

30
00:02:03,840 --> 00:02:09,330
So let's go ahead and add a template view for each of those we'll say add the H T.M. and that's going

31
00:02:09,330 --> 00:02:10,960
to have a form to add puppies.

32
00:02:11,910 --> 00:02:17,820
Then we'll create a new file here will say delete each T.M. that's going to last to remove puppies from

33
00:02:17,820 --> 00:02:18,780
our database.

34
00:02:20,070 --> 00:02:25,140
Finally we'll create a list that each Tamil which allows us to list puppies from our database.

35
00:02:25,230 --> 00:02:30,540
And these are the fundamental views you're going to have in any major Web site the ability to add information

36
00:02:30,540 --> 00:02:34,090
remove information or list information that you've already saved.

37
00:02:34,170 --> 00:02:35,780
So we have our template set up.

38
00:02:35,910 --> 00:02:42,580
Let's get started by setting up our adoption site up pie and then we'll start connecting it to our templates.

39
00:02:42,660 --> 00:02:47,220
Again there's no real right or wrong way as far as creating your templates first and then creating your

40
00:02:47,260 --> 00:02:51,770
thought script to connecting it through review functions or doing it the way we're going to do now.

41
00:02:51,770 --> 00:02:57,520
Setting up your python script first and then setting up the html files or doing a mix of match both.

42
00:02:57,630 --> 00:03:00,310
So I'm right now in adoption site that pie.

43
00:03:00,530 --> 00:03:01,770
Let's get started.

44
00:03:01,790 --> 00:03:07,530
The first thing I want to do is start with my imports at the very top.

45
00:03:07,570 --> 00:03:08,940
Import os.

46
00:03:09,520 --> 00:03:19,310
And then I'm going to at some point in time say from forms import and I'll create an add form and they

47
00:03:19,330 --> 00:03:20,230
delete form.

48
00:03:20,260 --> 00:03:25,180
So on keeping this commented out for now because if I take a look landforms up my file that's actually

49
00:03:25,180 --> 00:03:26,070
totally empty.

50
00:03:26,260 --> 00:03:36,670
So I'm going to later on add a form and add a delete form and I'll say from flask import flask and we're

51
00:03:36,670 --> 00:03:45,330
also going to be rendering templates will be using your L4 and then we'll also say redirect then will

52
00:03:45,330 --> 00:03:57,900
say from flask underscore sequel alchemy import sequel alchemy and then from flask underscore migrates.

53
00:03:58,170 --> 00:04:02,920
We're going to import migrate then we'll create our application.

54
00:04:03,020 --> 00:04:12,770
So I'll say flask name and then we're going to set up a key for forms soci app config and then we'll

55
00:04:12,770 --> 00:04:18,310
say create the secret key and you can set that equal to any string you want.

56
00:04:18,310 --> 00:04:23,960
So I'll say my secret key again this is just from the forms section of the course where we set up a

57
00:04:23,960 --> 00:04:24,970
secret key.

58
00:04:25,400 --> 00:04:35,840
So now we're going to do the sequel database and models part delicious combat this sequel database section.

59
00:04:35,990 --> 00:04:40,580
And as I mentioned often for larger projects you have this in a model's not pi script but we're going

60
00:04:40,580 --> 00:04:43,040
to talk about that in a future lecture Frinton else.

61
00:04:43,070 --> 00:04:44,950
Keep everything in one pie script.

62
00:04:45,290 --> 00:04:54,950
We'll set up our base there victory base directory set to OS path absolute path and then just as before

63
00:04:54,950 --> 00:05:02,560
we say OS path groud the current directory name and say underscore underscore file underscore underscore

64
00:05:04,750 --> 00:05:07,390
ok don't say app config.

65
00:05:08,360 --> 00:05:16,820
And then we'll say sequel alchemy underscore database underscore you are.

66
00:05:16,820 --> 00:05:27,220
I and I'm going to set that equal to sequel like colon three forward slashes plus OS that path.

67
00:05:27,230 --> 00:05:35,080
Join in I'm going to join my base directory with data dot sequel like OK.

68
00:05:35,110 --> 00:05:39,190
And just to get a little more room here going to collapse this left hand panel.

69
00:05:39,270 --> 00:05:39,730
There we go.

70
00:05:39,750 --> 00:05:40,710
So we have our base directory.

71
00:05:40,710 --> 00:05:42,720
There's one more configuration we do.

72
00:05:44,170 --> 00:05:53,530
And that's the track changes one so we'll say sequel alchemy underscore track modifications and I'm

73
00:05:53,530 --> 00:06:02,910
going to set that equal to false then we'll say create our database by passing in our application into

74
00:06:02,910 --> 00:06:07,830
sequel alchemy and then we're going to migrate the whole thing or basically give the ability to migrate

75
00:06:08,190 --> 00:06:12,150
connecting our application with our database.

76
00:06:12,150 --> 00:06:12,570
OK.

77
00:06:12,840 --> 00:06:15,930
So the next step after this is to actually create the models.

78
00:06:15,930 --> 00:06:18,590
So we just have one model in our particular site.

79
00:06:18,690 --> 00:06:20,360
It's going to be the model for a puppy.

80
00:06:20,370 --> 00:06:21,890
Let's go ahead and create that now.

81
00:06:23,230 --> 00:06:25,140
And it's up to you how much comments you want to put in here.

82
00:06:25,150 --> 00:06:31,300
But we'll say models and often this will be in a separate file called models not pie.

83
00:06:31,420 --> 00:06:41,600
But since we're just dealing with simplicity we'll just say class puppy D-B model moins create a table

84
00:06:41,600 --> 00:06:46,850
name for this we'll do it as last time we'll just say it's puppies.

85
00:06:46,950 --> 00:07:00,270
So make sure we set up puppies let's give each puppy a primary key id say D-B call them D-B integer

86
00:07:01,490 --> 00:07:02,170
integer

87
00:07:05,450 --> 00:07:13,740
and then for that we'll say primary key is equal to true and puppies.

88
00:07:13,740 --> 00:07:23,300
One other Actually you can give them as a name so say D-B column and we'll say D-B text.

89
00:07:23,320 --> 00:07:30,110
Ok so when you give in that method and arts create a puppy so IDF underscore underscore in that underscore

90
00:07:30,110 --> 00:07:39,550
underscore self and then name and they will say self taught name is equal to name and let's finally

91
00:07:39,550 --> 00:07:50,920
give a representation of the subject we'll say self and using string formatting we'll say return puppy

92
00:07:51,490 --> 00:07:58,740
name self-taught name string formatting.

93
00:07:58,810 --> 00:08:01,440
OK so that's our puppy's model.

94
00:08:01,450 --> 00:08:05,830
Up next we need to do is create the actual views and then connect them with the forms.

95
00:08:05,830 --> 00:08:08,290
But right now I think we've done enough for just one video.

96
00:08:08,440 --> 00:08:09,980
We'll continue this off in a part do.

97
00:08:09,980 --> 00:08:14,470
Bill let's do a quick review of what we've done so far in this video.

98
00:08:14,470 --> 00:08:20,290
We set up our template files and we set up our two pi scripts one of them is going to hold forms which

99
00:08:20,290 --> 00:08:26,470
will fill out at a later lecture in our adoption site that pile what we've done so far is we've done

100
00:08:26,470 --> 00:08:27,430
our imports.

101
00:08:27,430 --> 00:08:32,080
We're still waiting on one for our form and delete form creator application.

102
00:08:32,290 --> 00:08:36,160
Did the configuration so that we can successfully use forms that are templates.

103
00:08:36,160 --> 00:08:41,590
Set up our SQL database configuration set up our DVR checked as well as set up migrations and then we

104
00:08:41,590 --> 00:08:47,290
created this puppy model which later on we'll see how we can connect to it add puppies and then delete

105
00:08:47,320 --> 00:08:48,670
and remove puppies as well.

106
00:08:48,880 --> 00:08:51,760
All right we're going to continue this in part 2 right where we left off.

107
00:08:51,760 --> 00:08:52,800
We'll see you at the next lecture.
