1
00:00:05,360 --> 00:00:08,450
Welcome back, everyone, to this section on user authentication.

2
00:00:09,290 --> 00:00:13,460
Well, we're going to do now is explore how to handle users and sessions with Django's built in tools.

3
00:00:13,940 --> 00:00:18,050
Well, create a simple library website where users have profiles and books.

4
00:00:18,440 --> 00:00:20,630
Let's get started with a simple skeleton of the site.

5
00:00:20,900 --> 00:00:22,220
I'm going to head over to voice code.

6
00:00:22,700 --> 00:00:22,910
All right.

7
00:00:22,910 --> 00:00:27,830
So here I am in my Django Lectures folder, which is on my desktop, which is completely empty right

8
00:00:27,830 --> 00:00:28,130
now.

9
00:00:28,520 --> 00:00:30,490
And what I'm going to do is just start a project.

10
00:00:30,500 --> 00:00:31,850
So let's say Django admin.

11
00:00:33,130 --> 00:00:37,330
Start project and I'm going to start my library project.

12
00:00:38,310 --> 00:00:41,010
Go ahead and hit enter and then you should see a pop up.

13
00:00:41,010 --> 00:00:41,700
So you can now see.

14
00:00:43,170 --> 00:00:44,550
Into your library.

15
00:00:45,060 --> 00:00:50,100
So you notice we have our library and remember that double use case of the directory.

16
00:00:50,100 --> 00:00:53,640
So a library underneath a library directory, then you have your settings image.

17
00:00:53,650 --> 00:00:59,130
Okay, so now what I'm going to do is just create a simple app for the catalog.

18
00:00:59,340 --> 00:01:03,630
So for example, in the catalogs of books, so we're just setting up the basic skeleton here.

19
00:01:04,110 --> 00:01:09,330
So what I'm going to do is, first off, make sure you're actually inside of library so you can run

20
00:01:09,330 --> 00:01:09,960
the following.

21
00:01:10,290 --> 00:01:18,360
I'll say Python, manage that pie and I'm going to start an app called Catalog Enter and you should

22
00:01:18,360 --> 00:01:25,260
see Beyond Library your actual catalog application that can then hold things like models for books or

23
00:01:25,260 --> 00:01:29,580
views for actually expressing different things that are inside your catalog.

24
00:01:30,180 --> 00:01:34,950
Now, as we continue, we want to make sure that we actually register the catalog application.

25
00:01:34,950 --> 00:01:39,990
Sometimes we do this later on, but typically it's nice just to immediately after creating the application,

26
00:01:40,110 --> 00:01:41,460
go ahead and register it.

27
00:01:41,850 --> 00:01:48,000
Remember inside settings dot pie, if you scroll down, you have the installed apps.

28
00:01:48,000 --> 00:01:51,000
So that's where we actually want to add in the catalog.

29
00:01:51,000 --> 00:01:52,710
So we're going to say here.

30
00:01:53,770 --> 00:01:55,450
And make sure you don't forget that comma.

31
00:01:56,350 --> 00:02:02,890
I'm going to put this on a new line so it looks like everything else I'm going to say catalog the app's

32
00:02:03,610 --> 00:02:05,470
dot catalog.

33
00:02:05,680 --> 00:02:07,840
Config catalog.

34
00:02:09,470 --> 00:02:10,220
Config.

35
00:02:10,310 --> 00:02:13,520
And if you're wondering, where do you actually get this line of code?

36
00:02:13,790 --> 00:02:16,460
Remember, if you go to app stop pie, you see it right here.

37
00:02:16,910 --> 00:02:22,070
Catalog config obviously could be different depending on what you decided to name your application.

38
00:02:22,430 --> 00:02:27,070
So we can see we have inside catalog the app's catalog config.

39
00:02:27,080 --> 00:02:31,170
So if you come here, you have catalog, app store catalog config.

40
00:02:31,490 --> 00:02:31,790
All right.

41
00:02:32,000 --> 00:02:33,650
So that's where I'm getting that from.

42
00:02:34,070 --> 00:02:37,700
So we can go ahead and save those changes inside settings that py.

43
00:02:38,000 --> 00:02:40,880
Later on we'll be doing things like running migrations, etc..

44
00:02:42,240 --> 00:02:47,010
And since we're probably going to have views associated just with the catalog, let's begin mapping

45
00:02:47,010 --> 00:02:48,450
our URL's file.

46
00:02:48,720 --> 00:02:56,340
So what I'll do here is I'm going to go to URLs, dot pie and eventually expand this to actually link

47
00:02:56,340 --> 00:02:57,540
to the catalog.

48
00:02:57,870 --> 00:03:03,120
Now to do that, I have to actually create my URLs that py file inside of catalog.

49
00:03:03,510 --> 00:03:09,900
So inside of catalog I'm going to say new file and as we've done before, call it urls dot py.

50
00:03:10,350 --> 00:03:14,140
So this is the URLs for my catalog views.

51
00:03:14,340 --> 00:03:15,450
So just keep that in mind.

52
00:03:15,750 --> 00:03:22,080
But now that I have this URL, let's just go ahead and quickly link it at a settings or project level.

53
00:03:22,500 --> 00:03:23,730
So we already know how to do this.

54
00:03:23,730 --> 00:03:24,720
We've done this a bunch of times.

55
00:03:25,140 --> 00:03:30,390
So we're going to say import path, but also remember to import include and let's use it here.

56
00:03:31,360 --> 00:03:32,140
We're going to say path.

57
00:03:33,370 --> 00:03:34,600
Goes to catalog.

58
00:03:35,850 --> 00:03:36,600
And zoom in here.

59
00:03:36,600 --> 00:03:37,380
One more for you.

60
00:03:38,310 --> 00:03:39,600
So you can really see what I'm typing here.

61
00:03:39,600 --> 00:03:49,170
Catalog for Slash and then I'm going to include and this is where we can say catalog your URLs and don't

62
00:03:49,170 --> 00:03:50,670
forget your comma here.

63
00:03:51,150 --> 00:03:54,210
Always be careful of commas when you're doing things with lists.

64
00:03:54,210 --> 00:03:58,320
These are kind of hard to debug sometimes, so we'll go ahead and save that change.

65
00:03:58,350 --> 00:04:03,840
Added in the connection to the path for you URL patterns from my catalog application.

66
00:04:05,260 --> 00:04:10,390
And one of the things I'm going to do here that we haven't really done before is actually automatically

67
00:04:10,390 --> 00:04:13,030
redirect the homepage to go to catalog.

68
00:04:13,450 --> 00:04:17,709
So just to give you an example, typically when we've been actually working on our projects, we'll

69
00:04:17,709 --> 00:04:22,570
do something like this example, you know, dot com, and then they have to go forward, slash to the

70
00:04:22,570 --> 00:04:23,810
app that we're working with.

71
00:04:24,160 --> 00:04:30,130
But wouldn't it be nice if we could just go straight from example dot com and then for them to.

72
00:04:30,400 --> 00:04:32,590
Example dot com forward slash app.

73
00:04:33,010 --> 00:04:38,170
Typically we've been living or leaving the actual index page blank and then having to manually type

74
00:04:38,170 --> 00:04:39,550
out that first application.

75
00:04:39,880 --> 00:04:45,160
What I'm going to do is just have an automatic permanent redirect to go from the base domain to the

76
00:04:45,160 --> 00:04:50,290
actual application where it's going to be local host goes to local host forward slash catalog.

77
00:04:50,800 --> 00:04:55,630
Now we already know about some built in views and built in models and just built an import.

78
00:04:55,630 --> 00:05:02,470
So jingo, the easiest way to do a redirect permanently is the following way we can say and you have

79
00:05:02,470 --> 00:05:05,020
to actually import this from generic fields or say from.

80
00:05:06,090 --> 00:05:12,810
Jingle dot views dot generic imports a redirect view.

81
00:05:13,780 --> 00:05:17,800
And then here inside the URL patterns, you just need to put in this code.

82
00:05:17,800 --> 00:05:18,730
You're going to say path.

83
00:05:20,480 --> 00:05:23,240
The home path, which is just nothing or but that's the local host.

84
00:05:24,020 --> 00:05:26,180
And then you're going to say redirect view.

85
00:05:27,670 --> 00:05:28,510
As few.

86
00:05:29,940 --> 00:05:32,130
And you're going to pass in the URL to redirect to.

87
00:05:32,880 --> 00:05:37,830
Obviously, we still need to fill out those views, but just doing this ahead of time, we should now

88
00:05:37,830 --> 00:05:45,570
with this line of code, be able to redirect the blank, which is essentially the domain to this blank

89
00:05:45,570 --> 00:05:46,950
forward slash catalog.

90
00:05:47,250 --> 00:05:47,610
Okay.

91
00:05:47,850 --> 00:05:54,420
So we still need to actually set up our primary view for the catalog, inside views and URLs, but so

92
00:05:54,420 --> 00:05:56,250
far we're now actually redirecting.

93
00:05:56,250 --> 00:05:57,780
So make sure to save those changes.

94
00:05:59,040 --> 00:06:03,570
And now to finish this off, let's create a very basic view for the catalog home page.

95
00:06:04,090 --> 00:06:05,590
So we'll come back to views here.

96
00:06:05,710 --> 00:06:10,950
I'm going to create just a very simple view that essentially just shows some sort of version of Hello,

97
00:06:10,960 --> 00:06:12,550
just so we can confirm that this is all working.

98
00:06:13,510 --> 00:06:17,200
So I'm actually going to go first to your URLs that PY and let's go ahead and set this up.

99
00:06:17,860 --> 00:06:26,800
So we're going to say from Django, dot URLs, import path and then we can say from that import.

100
00:06:27,910 --> 00:06:28,420
Views.

101
00:06:29,430 --> 00:06:31,260
And set up our URL patterns.

102
00:06:33,000 --> 00:06:36,210
This is early patterns for just the catalog app.

103
00:06:36,720 --> 00:06:37,950
So here I'm going to say path.

104
00:06:39,720 --> 00:06:40,650
Homepage here.

105
00:06:41,990 --> 00:06:42,970
View start.

106
00:06:43,010 --> 00:06:45,110
And let's just make this the index view.

107
00:06:46,100 --> 00:06:47,300
Give it the name index.

108
00:06:50,330 --> 00:06:54,980
And now after we save that, I can create my index view since I know its name though.

109
00:06:55,430 --> 00:06:59,990
So say the index and then we're going to do is taking the request here.

110
00:07:01,180 --> 00:07:03,520
And then you can just return just a simple hello.

111
00:07:03,550 --> 00:07:06,040
You don't need to return an entire template right now or work on that later.

112
00:07:07,190 --> 00:07:10,040
So then to show that, let's just actually show a simple HTP response.

113
00:07:10,040 --> 00:07:15,980
We'll say from Django, the HTP import HTP response.

114
00:07:17,940 --> 00:07:18,420
There we go.

115
00:07:18,930 --> 00:07:20,760
And then say here.

116
00:07:21,450 --> 00:07:23,550
Return to the pure response.

117
00:07:24,120 --> 00:07:24,790
Something like.

118
00:07:25,590 --> 00:07:25,980
Hello.

119
00:07:27,030 --> 00:07:27,330
Okay.

120
00:07:27,690 --> 00:07:32,670
So we've done a lot so far, but basically nothing we haven't really seen before, minus the little

121
00:07:32,670 --> 00:07:34,770
quick fix of redirecting the view.

122
00:07:34,800 --> 00:07:36,210
So we have a little skeleton.

123
00:07:36,540 --> 00:07:41,730
And now we should be at a point where we can then start adding tons of URLs, views and models to hold

124
00:07:41,730 --> 00:07:42,210
everything.

125
00:07:42,510 --> 00:07:45,060
I do, however, want to make sure this is actually all working.

126
00:07:45,600 --> 00:07:51,900
So we're going to go here and say go ahead and Python manage the API and then let's run the server.

127
00:07:52,380 --> 00:07:53,700
So going to say Python.

128
00:07:54,600 --> 00:07:56,520
Manage that pie run server.

129
00:07:56,520 --> 00:07:58,800
Let's make sure we don't actually have any typos on anything.

130
00:07:59,550 --> 00:08:04,560
So we haven't run migrations yet, but it looks like we do have something on our local.

131
00:08:04,890 --> 00:08:10,480
So I'm going to go to our local on my actual computer here.

132
00:08:10,480 --> 00:08:14,160
So let me bring in my browser and it's going to redirect to here.

133
00:08:14,640 --> 00:08:19,050
And if you do that, you should get the automatic redirect to something that just says hello.

134
00:08:19,800 --> 00:08:21,300
So here I can see hello.

135
00:08:22,140 --> 00:08:25,130
And if you look at this, you should have seen the redirect happen.

136
00:08:25,140 --> 00:08:30,360
So to get HTTP 1.1, it gives us a three or two redirect to catalog.

137
00:08:30,720 --> 00:08:32,429
And then here we have this.

138
00:08:32,669 --> 00:08:34,110
Okay, so it looks like everything's working.

139
00:08:34,500 --> 00:08:40,559
And now what we can do is in the next lecture, begin focusing on our models for everything in the catalog.

140
00:08:40,860 --> 00:08:41,400
I'll see you there.

