1
00:00:05,190 --> 00:00:08,340
Welcome back, everyone, to this lecture on how Django works.

2
00:00:09,630 --> 00:00:14,760
We're going to be discussing the key ideas behind how the Django framework actually operates and works.

3
00:00:15,270 --> 00:00:19,800
This is also going to lead to a nice roadmap of our learning journey for the rest of the course.

4
00:00:21,270 --> 00:00:27,540
So the key features of jangle fall under what is usually called a model template view structure.

5
00:00:27,930 --> 00:00:32,970
So you have your model, which connects to the actual database, your template which connects to the

6
00:00:32,970 --> 00:00:37,560
user browser and then your view, which is kind of a connection between the model and the template.

7
00:00:37,980 --> 00:00:43,350
And in general terms, there's going to be a couple of features that allows us to follow this MTV structure

8
00:00:43,680 --> 00:00:46,780
and that is an object relational mapper or 000rpm.

9
00:00:47,220 --> 00:00:53,160
The Python models themselves written with Django, the URLs and views, and then the templates.

10
00:00:53,610 --> 00:00:55,770
So let's see how this actually plays out.

11
00:00:56,010 --> 00:01:01,320
If we're trying to connect information from a database somewhere, such as a database hosted on Leno

12
00:01:01,650 --> 00:01:03,000
to a user browser.

13
00:01:04,040 --> 00:01:10,490
So what we have here is the connection we're trying to do, get information from the database all the

14
00:01:10,490 --> 00:01:14,180
way to the user browser and then clearly there's going to be some interactivity.

15
00:01:14,270 --> 00:01:18,650
And you can think that every web application at its core is doing something similar to this.

16
00:01:18,650 --> 00:01:22,070
It's transferring information from a database to a user.

17
00:01:22,340 --> 00:01:28,820
And then the user can update or transfer information back to the database, and information can be videos,

18
00:01:28,820 --> 00:01:31,910
pictures, text activities, cetera.

19
00:01:32,390 --> 00:01:38,360
OK, so let's start backwards from the user browser, and we're going to be exploring this in terms

20
00:01:38,360 --> 00:01:40,920
of actual Django framework specifics.

21
00:01:42,590 --> 00:01:48,470
So your user browser is going to end up receiving what's known as a template, and the template itself

22
00:01:48,800 --> 00:01:52,430
is going to be an HTML file, so the template is HTML.

23
00:01:52,760 --> 00:01:59,570
But keep in mind, a dot HTML file can then link to things like a CSV file or a JavaScript file, so

24
00:01:59,570 --> 00:02:05,060
you can understand the template as what your browser is directly reading for information of what to

25
00:02:05,060 --> 00:02:07,160
display to the user.

26
00:02:07,670 --> 00:02:11,840
Now, how do we actually end up connecting the template to the database?

27
00:02:11,870 --> 00:02:18,140
Each HTML, CSS and JavaScript aren't really going to be stored as far as things that can be manipulated

28
00:02:18,140 --> 00:02:18,920
by the user.

29
00:02:19,280 --> 00:02:25,580
Instead, we're going to use ginger templating and note might use their of two curly sets of brackets

30
00:02:25,910 --> 00:02:30,320
because that's the sort of syntax you're going to be seeing when we actually discover how to work with

31
00:02:30,320 --> 00:02:31,160
ginger templating.

32
00:02:31,580 --> 00:02:38,840
But ginger itself allows us to insert information directly from a python file into that template.

33
00:02:39,470 --> 00:02:45,350
So this later connects to the actual Python file that's using the Django Library or Django framework.

34
00:02:45,530 --> 00:02:51,170
So we end up doing is we create a view component, and this is typically going to be something called

35
00:02:51,350 --> 00:02:58,400
viewport pie is the python file, and this python file is essentially a list of functions and the use

36
00:02:58,400 --> 00:02:59,240
of decorators.

37
00:02:59,510 --> 00:03:06,470
It's going to allow us to inject information into that template directly from Python, which this allows

38
00:03:06,470 --> 00:03:12,350
us to use all different types of Python libraries to do any sort of analysis or manipulation with the

39
00:03:12,350 --> 00:03:17,780
data of a Python library, and then inject that information directly into the template in a form that

40
00:03:17,780 --> 00:03:19,550
the user browser can then understand.

41
00:03:20,480 --> 00:03:25,700
The other thing to note is that this view works in concert with some URL routing.

42
00:03:26,000 --> 00:03:30,710
Typically, this is going to be another Python file, often called URLs that pie.

43
00:03:30,740 --> 00:03:34,490
And this just tells which view goes to which URL route.

44
00:03:34,610 --> 00:03:41,450
So one view may be associated to my website dot com forward slash stuff, and maybe another view goes

45
00:03:41,450 --> 00:03:45,170
to another subdomain like my website dot com for slash users.

46
00:03:45,530 --> 00:03:51,050
So you can see here it's going to be interactivity between the views and which route they should take,

47
00:03:51,350 --> 00:03:55,400
which connects them to a specific template which is then shown to the user browser.

48
00:03:55,820 --> 00:04:00,020
And keep in mind, right now I'm showing everything kind of going towards the user browser, but a lot

49
00:04:00,020 --> 00:04:02,540
of this will have back and forth interconnectivity.

50
00:04:03,020 --> 00:04:07,310
So where does the actual information connect to the database?

51
00:04:08,030 --> 00:04:15,830
Well, the view information itself is often linked to a model, and models are going to be a subset

52
00:04:15,830 --> 00:04:22,280
of the jingo framework that is actually a python representation of a database and database tables.

53
00:04:22,400 --> 00:04:27,170
So models, it's going to be yet another Python file again, often called models, not pie.

54
00:04:27,440 --> 00:04:32,780
And this is using Django models, which are essentially python classes where you have these different

55
00:04:32,780 --> 00:04:38,600
attributes inside the class that end up representing different columns within a table within a database.

56
00:04:38,930 --> 00:04:42,310
So that's actually going to end up linking directly to your database.

57
00:04:42,320 --> 00:04:48,050
And what's really nice about Django is by just working with the models file often Django, you pretty

58
00:04:48,050 --> 00:04:53,360
much don't have to worry about all the sequel and database management that's happening behind the scenes.

59
00:04:53,660 --> 00:04:59,360
That's what makes Django so powerful is that even though this looks really complex, you just end up

60
00:04:59,360 --> 00:05:05,990
dealing with a couple of Python files using Python code, and Django itself is doing all the heavy lifting

61
00:05:05,990 --> 00:05:11,750
for you on the back end by actually setting stuff up with a sequel database, making sure you are all

62
00:05:11,750 --> 00:05:14,210
routings, connecting to your views and so on.

63
00:05:15,020 --> 00:05:20,150
So this is the main pathway that we would actually follow to get information from a database to the

64
00:05:20,150 --> 00:05:20,870
user browser.

65
00:05:21,170 --> 00:05:27,950
We have a database typically stored in something like sequel and then that is directly linked to a model's

66
00:05:27,950 --> 00:05:31,610
file that's using the Django framework, and that itself is a Python file.

67
00:05:31,880 --> 00:05:38,450
And then we can connect those models to different views, and those views through a URL routing configuration

68
00:05:38,750 --> 00:05:44,240
are then allowing us to insert information into a template that the user can then see in their browser.

69
00:05:45,580 --> 00:05:49,990
Now, if you're more familiar with terms like what's happening on the client side versus the server

70
00:05:49,990 --> 00:05:55,780
side, you can think of the template as the client side because it's actually happening on the user's

71
00:05:55,780 --> 00:05:56,290
computer.

72
00:05:56,530 --> 00:06:01,330
They're actually seeing the HTML, the CSC, JavaScript and then what's inserted through ginger, which

73
00:06:01,330 --> 00:06:06,670
eventually just becomes normal HTML on the user browser side and then everything else is happening on

74
00:06:06,670 --> 00:06:07,420
the server side.

75
00:06:07,660 --> 00:06:12,700
So everything that really has to do with import Django and do something is happening on the server side

76
00:06:12,700 --> 00:06:18,130
of the URL, routing the views and the models and eventually further back the database, which is storing

77
00:06:18,130 --> 00:06:20,680
all the information that's connected through the models.

78
00:06:22,110 --> 00:06:27,600
So if we think about this back in terms of model template view structure, we have the Model S., which

79
00:06:27,600 --> 00:06:30,540
is kind of this model's connection to a database.

80
00:06:31,260 --> 00:06:36,390
You have the template side of things which is going to be your HTML template that is using gender templating

81
00:06:36,390 --> 00:06:42,330
to actually then connect to the view section, which you can kind of think of as the view that's actually

82
00:06:42,330 --> 00:06:44,460
working with the data connecting to your routing.

83
00:06:44,760 --> 00:06:49,830
And it's the connection between the model on the very back and the things versus the template on the

84
00:06:49,830 --> 00:06:52,020
front and the things for the actual client side.

85
00:06:52,360 --> 00:06:54,720
This is where you get that model view template structure.

86
00:06:56,210 --> 00:07:00,920
And again, I mentioned that this was all diagram showing a one way connection to the database of the

87
00:07:00,920 --> 00:07:01,640
user browser.

88
00:07:01,940 --> 00:07:06,950
But keep in mind that the models and database those are actually going to have interconnectivity.

89
00:07:07,160 --> 00:07:10,310
So you can update models which and updates your sequel database.

90
00:07:10,640 --> 00:07:16,100
And you should also keep in mind that you can have many more Python files for application logic so you

91
00:07:16,100 --> 00:07:21,200
can have something like that pie, which maybe does some machine learning logic, and you just connect

92
00:07:21,200 --> 00:07:24,230
that through import to your final viewport pie file.

93
00:07:24,680 --> 00:07:30,080
So there's going to be lots of interconnectivity between models having logic applied to them in Python

94
00:07:30,080 --> 00:07:31,490
and then sending that to The View.

95
00:07:31,730 --> 00:07:35,990
Or maybe you just send the models directly to the View, or maybe you're using some other application

96
00:07:35,990 --> 00:07:38,360
logic within your Python file, cetera.

97
00:07:38,630 --> 00:07:44,240
Main idea here being is that this is where you can really expand the flexibility of Python by adding

98
00:07:44,240 --> 00:07:49,970
in as many Python scripts as you want and performing many different types of manipulation, analysis,

99
00:07:50,240 --> 00:07:53,900
comprehension of the data, et cetera, through Python applications.

100
00:07:53,900 --> 00:07:59,480
And eventually, you send that information to the view that pie file, which itself is connected with

101
00:07:59,480 --> 00:08:06,110
URL routing to eventually serve that information through Jinja templating, which can then be sent to

102
00:08:06,110 --> 00:08:07,070
the user.

103
00:08:07,160 --> 00:08:09,380
And obviously, this goes both ways.

104
00:08:09,380 --> 00:08:13,670
The user can then update information, which is then sent to the View, which updates the model, which

105
00:08:13,670 --> 00:08:14,720
updates the database.

106
00:08:14,720 --> 00:08:20,300
So it's not just one way the user can update, as well as Django and Python reporting back to the user

107
00:08:20,570 --> 00:08:23,210
what the current status of things in the database is.

108
00:08:23,750 --> 00:08:24,110
All right.

109
00:08:25,030 --> 00:08:30,010
So the jingo structure will also have many more features not actually shown in that simple MTV diagram,

110
00:08:30,280 --> 00:08:33,010
such as authentication and administration access.

111
00:08:33,309 --> 00:08:37,600
So something we didn't really talk about here is the fact that Django comes with tools to make sure

112
00:08:37,809 --> 00:08:41,860
that user has to be authenticated in order to view certain parts of a website.

113
00:08:42,159 --> 00:08:47,380
Or maybe there's also administration access that comes already built in of Django, which allows a super

114
00:08:47,380 --> 00:08:53,080
user to just directly jump to the database and see individual entries inside that actual SQL database.

115
00:08:53,350 --> 00:08:57,940
And you'll see how awesome this feature is when it's pretty much it's just created for you automatically

116
00:08:58,210 --> 00:08:59,590
from the Django framework.

117
00:09:01,070 --> 00:09:05,540
So what are the actual jingo drawbacks, of course, right now, everything's super great.

118
00:09:05,900 --> 00:09:13,190
Well, keep in mind that jingo itself is heavily reliant on this idea of that model, which connects

119
00:09:13,190 --> 00:09:19,040
to the database and the model is this Python or Django representation of a table in that database?

120
00:09:19,550 --> 00:09:24,470
What is really nice about this is that it makes it very easy to work with querying the data, but it

121
00:09:24,470 --> 00:09:29,630
does add the requirements of understanding models pretty well and then setting them up for views.

122
00:09:29,960 --> 00:09:32,600
So this is sometimes the steeper part of the learning curve.

123
00:09:32,870 --> 00:09:37,250
Is that Django, in order to do a lot of the heavy lifting for you by setting up your SQL database,

124
00:09:37,550 --> 00:09:43,130
those require the user itself to have some knowledge of how these models work within Django.

125
00:09:43,520 --> 00:09:47,120
Now, of course, the purpose of this course itself is to make that easy for you.

126
00:09:47,480 --> 00:09:50,360
So I wouldn't worry too much about the learning curve as a drawback.

127
00:09:50,630 --> 00:09:55,010
Just keep in mind that there is this heavy reliance on the model structure, but I think when you get

128
00:09:55,010 --> 00:10:00,110
more and more familiar of Django, you'll actually like this idea of having everything going through

129
00:10:00,110 --> 00:10:06,380
the model because it applies a very specific workflow to the website or web application itself.

130
00:10:07,760 --> 00:10:12,530
So what we can do now is continue our understanding of jingo by beginning to learn about these key components

131
00:10:12,530 --> 00:10:16,940
separately and then slowly adding on complexity to a fully functioning website.

132
00:10:17,330 --> 00:10:17,690
OK.

133
00:10:17,930 --> 00:10:19,430
Thanks, and I'll see you at the next lecture.

