1
00:00:00,260 --> 00:00:01,300
Hey, welcome back.

2
00:00:01,310 --> 00:00:05,840
In this video, you will learn the concept of context in Django.

3
00:00:05,930 --> 00:00:09,560
Context is a key feature of Django.

4
00:00:09,800 --> 00:00:12,860
So why is context important?

5
00:00:13,010 --> 00:00:17,750
Well, you'll understand that by using an example, as always.

6
00:00:17,750 --> 00:00:21,380
So let's say that currently we have this page.

7
00:00:21,380 --> 00:00:23,870
This is what we built previously, right?

8
00:00:23,870 --> 00:00:30,020
So in this page we want to display the meals, pizza, pasta, etcetera.

9
00:00:30,020 --> 00:00:34,580
And these meals, these data will be stored in the database.

10
00:00:34,580 --> 00:00:38,390
So Python will have access to the database, right?

11
00:00:38,420 --> 00:00:42,320
So for example, in Views.py we have Python code.

12
00:00:42,320 --> 00:00:49,430
So that means we need to get those data from Python to HTML.

13
00:00:49,520 --> 00:00:51,860
So from Python to.

14
00:00:54,290 --> 00:00:56,950
XHTML index.html.

15
00:00:56,960 --> 00:01:00,680
So this here is produced by index.html.

16
00:01:00,710 --> 00:01:04,700
So to get those data we use the context.

17
00:01:06,240 --> 00:01:07,980
So that's a bold theory.

18
00:01:08,010 --> 00:01:10,490
Now, let's implement the context here.

19
00:01:10,500 --> 00:01:12,960
So we're talking about this page, right?

20
00:01:12,990 --> 00:01:17,700
The home page, which is represented by this view.

21
00:01:17,730 --> 00:01:23,260
So now in this view, we create a function, a method.

22
00:01:23,280 --> 00:01:33,450
So this is a method since it's inside a class, A get context data, you have to name this method exactly

23
00:01:33,450 --> 00:01:40,140
like this gets underscore context data because this is basically a predefined method.

24
00:01:40,140 --> 00:01:43,770
It's a method that is part of list view of this class.

25
00:01:43,770 --> 00:01:49,290
So we are overriding this method by reusing that name.

26
00:01:49,980 --> 00:01:55,080
So this has to have a self argument since it's inside the class.

27
00:01:55,080 --> 00:01:59,460
And then inside here we create a context variable.

28
00:01:59,460 --> 00:02:02,160
This is a context I was talking about.

29
00:02:02,190 --> 00:02:13,630
Now this context is a dictionary and this dictionary can contain keys and values, let's say pizza here.

30
00:02:14,510 --> 00:02:15,500
In this example.

31
00:02:15,500 --> 00:02:20,090
It's a dictionary of one key and one value for that key.

32
00:02:20,420 --> 00:02:23,120
And then you return that context.

33
00:02:23,200 --> 00:02:26,220
Then you go to your index.html.

34
00:02:26,270 --> 00:02:28,640
Let's, let's split this to the right.

35
00:02:28,640 --> 00:02:35,990
And somewhere in between the body tags, you want to display that data.

36
00:02:35,990 --> 00:02:48,800
For example, let's write some h three tags and inside this h three tags you write this jinja to syntax,

37
00:02:48,800 --> 00:02:51,680
and in between you write meals.

38
00:02:51,860 --> 00:02:55,310
So this corresponds to the key here.

39
00:02:55,400 --> 00:03:00,320
And then you go to the website, refresh it, make sure you are running the app.

40
00:03:00,320 --> 00:03:09,290
So I'm running the app Currently you can run the app using Python Manage.py run server and so refresh

41
00:03:09,290 --> 00:03:12,320
the page and there we go, we get pizza.

42
00:03:12,890 --> 00:03:22,110
So what I just did here is that I had this string pizza inside Python and I was able to send that string

43
00:03:22,110 --> 00:03:24,000
into the front end here.

44
00:03:24,000 --> 00:03:26,100
So this is just a static string.

45
00:03:26,100 --> 00:03:33,030
But normally we're going to get this data from the database using some more lines of code here.

46
00:03:33,480 --> 00:03:38,280
But for now, let's just create some static strings in here.

47
00:03:38,280 --> 00:03:49,680
So just to demonstrate you that you can send Python values from your python code to your HTML code just

48
00:03:49,680 --> 00:03:50,460
like that.

49
00:03:51,090 --> 00:03:56,430
So the context more technically now we can speak in more technical terms.

50
00:03:56,430 --> 00:04:02,130
The context is a dictionary made of keys and values.

51
00:04:02,340 --> 00:04:07,950
In this example, it has only one key meals and this has this value.

52
00:04:07,980 --> 00:04:09,990
So the context is a dictionary.

53
00:04:09,990 --> 00:04:18,089
And then from your HTML templates you can access the keys of that dictionary as variables.

54
00:04:18,089 --> 00:04:21,060
So you see no quotes around meals.

55
00:04:21,450 --> 00:04:24,720
And then this will get the value of this.

56
00:04:24,720 --> 00:04:26,130
This can also be a list.

57
00:04:26,130 --> 00:04:31,740
So you can say pizza, comma, pasta, right?

58
00:04:31,770 --> 00:04:38,550
If you refresh the page here, you're going to get a list and then I'll show you techniques how to iterate

59
00:04:38,550 --> 00:04:46,850
over that list to to show those items one by one here as bullet points or wherever you like.

60
00:04:46,860 --> 00:04:54,120
And of course, we can put a comma here and maybe like expand the dictionary in the next line and you

61
00:04:54,120 --> 00:04:54,960
can say

62
00:04:56,940 --> 00:05:03,990
ingredients, comma, and then you have a list of other things, things here.

63
00:05:05,100 --> 00:05:11,460
So this here ingredients can also be accessed.

64
00:05:14,290 --> 00:05:17,080
Similarly using the same syntax.

65
00:05:19,740 --> 00:05:20,610
Like that.

66
00:05:21,890 --> 00:05:22,670
Refresh.

67
00:05:22,670 --> 00:05:25,370
And here we have the value of ingredients.

68
00:05:26,650 --> 00:05:31,500
So that's how you send data from your Python code to HTML.

69
00:05:31,540 --> 00:05:39,370
And then next to what's next is that we want to create an admin interface so that we are able to add

70
00:05:39,370 --> 00:05:41,650
some records to our database table.

71
00:05:41,650 --> 00:05:43,510
So meals with their ingredients.

72
00:05:43,510 --> 00:05:49,290
And then we want to display those data here on the web page.

73
00:05:49,300 --> 00:05:51,610
So that's about it for now.

74
00:05:51,640 --> 00:05:53,040
I'll see you in the next videos.

75
00:05:53,050 --> 00:05:53,530
Thanks.

