Hey, welcome back.
In this video, you will learn the concept of context in Django.
Context is a key feature of Django.
So why is context important?
Well, you'll understand that by using an example, as always.
So let's say that currently we have this page.
This is what we built previously, right?
So in this page we want to display the meals, pizza, pasta, etcetera.
And these meals, these data will be stored in the database.
So Python will have access to the database, right?
So for example, in Views.py we have Python code.
So that means we need to get those data from Python to HTML.
So from Python to.
XHTML index.html.
So this here is produced by index.html.
So to get those data we use the context.
So that's a bold theory.
Now, let's implement the context here.
So we're talking about this page, right?
The home page, which is represented by this view.
So now in this view, we create a function, a method.
So this is a method since it's inside a class, A get context data, you have to name this method exactly
like this gets underscore context data because this is basically a predefined method.
It's a method that is part of list view of this class.
So we are overriding this method by reusing that name.
So this has to have a self argument since it's inside the class.
And then inside here we create a context variable.
This is a context I was talking about.
Now this context is a dictionary and this dictionary can contain keys and values, let's say pizza here.
In this example.
It's a dictionary of one key and one value for that key.
And then you return that context.
Then you go to your index.html.
Let's, let's split this to the right.
And somewhere in between the body tags, you want to display that data.
For example, let's write some h three tags and inside this h three tags you write this jinja to syntax,
and in between you write meals.
So this corresponds to the key here.
And then you go to the website, refresh it, make sure you are running the app.
So I'm running the app Currently you can run the app using Python Manage.py run server and so refresh
the page and there we go, we get pizza.
So what I just did here is that I had this string pizza inside Python and I was able to send that string
into the front end here.
So this is just a static string.
But normally we're going to get this data from the database using some more lines of code here.
But for now, let's just create some static strings in here.
So just to demonstrate you that you can send Python values from your python code to your HTML code just
like that.
So the context more technically now we can speak in more technical terms.
The context is a dictionary made of keys and values.
In this example, it has only one key meals and this has this value.
So the context is a dictionary.
And then from your HTML templates you can access the keys of that dictionary as variables.
So you see no quotes around meals.
And then this will get the value of this.
This can also be a list.
So you can say pizza, comma, pasta, right?
If you refresh the page here, you're going to get a list and then I'll show you techniques how to iterate
over that list to to show those items one by one here as bullet points or wherever you like.
And of course, we can put a comma here and maybe like expand the dictionary in the next line and you
can say
ingredients, comma, and then you have a list of other things, things here.
So this here ingredients can also be accessed.
Similarly using the same syntax.
Like that.
Refresh.
And here we have the value of ingredients.
So that's how you send data from your Python code to HTML.
And then next to what's next is that we want to create an admin interface so that we are able to add
some records to our database table.
So meals with their ingredients.
And then we want to display those data here on the web page.
So that's about it for now.
I'll see you in the next videos.
Thanks.