Welcome everyone to this lecture on rendering templates and context variables.
Now we've already seen a little bit of how to actually render a template by connecting a render function
call to an actual template or HTML file.
Well, we still haven't explored yet is how to send information from the Python object to view over
to the template.
And that's for that context.
Variable comes into play with the Django template language or the Djindjic template language, depending
on which you prefer to use.
But right now, we're going to show here it's going to work in both situations with either template
markup language.
So let's go ahead and check out this in visual studio code.
OK.
Here I am back at Visual Studio Code.
What I'm going to do to get a little bit of practice with connecting to a template is quickly create
a new template and then connect a view to it.
So how do we do this as a review if this template will be associated, if my app, then underneath my
app templates my app, I'll simply create a new file and I'm going to call this variable HTML since
variables is we're going to be talking about right now.
Then I should be able to simply type doc, enter, get the autocomplete and let's just have a quick
heading here in order to know that we're looking at the variable each time else, I'll say variable
the HTML save that.
So I have this ready to go.
Later on, we're going to keep adding to this template file.
Then I'll come back to views inside of app, and let's create a new view for this.
So we'll call it.
Variable view.
Take sent a request.
And it's going to return render from that request.
Underneath my app.
Forward slash variable, that HTML save that change later on will be dealing with the third argument,
which has a context mapping, so that's kind of what we're here about.
And before we do that, let's make sure it's connected underneath URLs.
So we have the example view as a path here.
What I'm going to do is add a comma.
A new path.
Let's have it be a variable.
So say variable forward slash and then connect this to views that.
Variable view.
OK, so once you have that running, you should be able to go back to your site, that's running, and
then we have the example h html at forward, slash my app, then go to forward slash variable and then
you should see variable HTML.
So we already know how to do this.
Well, we don't know really how to do yet is instead of just displaying HTML, what would make Django
really powerful is if I can do a bunch of logic, either, in my view, stop my file or in another Python
file that I've imported, exported from and eventually send that context back.
The way this works in the simplest manner is by working with what's known as a variable object.
That's kind of a vague term, but it's the formal term with Django.
And the way I like to think about it is just a context variable dictionary that gets sent from Python
to the actual template.
So keep in mind for what we're doing right now.
This has to be a dictionary.
So we're going to create.
A dictionary is going to call it my underscore variable.
And you can imagine that this is able to do any sort of calculations we want, and it's able to later
on take information from the request.
We'll actually have information, then take into account inside the Python variable, but we will do
that later on.
So just create a dictionary.
And inside this dictionary, I'm going to have a first name key.
And let's have the first name be something like Rosalind.
And then let's have the last name key.
B. Franklin.
So I have this little object here, it's a dictionary in Python, first name Rosalyn, last name Franklin.
Then what I want to do is be able to send this python variable over to be used instead of variable HTML.
And the way this works is you pass in a third argument called context, and then you set it equal to
my underscore variable or whatever you happen to call this variable again later on will do way more
complex versions of this of taking in objects that the user sends, doing whatever you want them in
Python and then sending them back, maybe redirecting to another view.
But for right now, keep it simple.
And we're just sending this variable dictionary.
Then the question arises How do we actually use it inside a variable HTML?
Well, the way this works is you go to variable to HTML and then you reference it with the syntax of
two sets of curly braces.
So let's say I'm going to do another heading here too.
Then you put in.
Two sets of curly braces, so two sets opening, two sets closing, and then you reference the key and
then the key is going to retrieve the value associated with it.
So that means I can say something like first underscore name inside of HTML.
And if I save that change and then refresh my page, I now see here, Rosalind.
So what is actually happening is keep this in mind from the views you have this dictionary being passed
back through the context and then inside the actual template itself.
You can begin to access this by referencing the keys.
And it doesn't have to be by itself.
You can also say something like her first name was.
And then the key is going to insert the value associated with it.
And then you can say her last name.
Was and then two sets curly brackets last underscore name, two sets, curly brackets go ahead and then
save that change.
Refresh your page and you should see that pop up to now.
Her first name was.
Her last name is Franklin.
OK, so that's pretty good right now.
Hopefully, it can get the gears turning of what's actually possible here.
We're going to expand this to actually show you, depending on the sort of Python object you sent back
within that dictionary.
You can begin to actually access attributes or indices from it.
So we're going to come back here to views that pie.
And I'm going to make this dictionary have some more key value pairs, so here inside this dictionary,
I'm going to create a key and I'm just going to call it some list and its associated value is going
to be a list one to three.
Let's make another key here.
It's going to be called.
Some dictionary.
Note that there's a dictionary within a dictionary, so there's really more for demonstration purposes.
You don't want to get this too confusing, so I'll say inside key.
And then call this inside.
Value.
OK.
Make sure your commas and formatting is correct here.
But basically what I've done is I have now some list one, two, three and some dictionary inside my
dictionary.
So I'm sending back this entire thing.
But then I should be able to access some list and some dictionary in the exact same way I did before.
So let's come back to variable and we have this first name, last name.
Let's actually grab that list.
So I can say again, two sets of curly braces simply say some list because that was the key.
Save that change.
And if I refresh, come back here.
I now see the list one to three being put there.
All right.
So just by itself, that's not super useful.
What I would like probably to do within a list is maybe loop through it, but we won't learn about that
yet.
It has to do with tax.
That's a little more complicated, but maybe right now I just want to grab a single item from that list.
The way you do that, to grab something that has an index capability, which means in Python, if you'd
be able to index it with something like some list.
Square braces zero.
So this is normal python indexing the way this works on the template side of things with Django template
language.
They come back to variable is you have to say Dot and then the index you want.
So for example, Dot zero save that that will grab the first item in that list.
So I refresh this here.
I now only see one.
No more square brackets.
So again, with the Django template language, you say dot zero.
In order to use that.
So that's indexing just to get some practice here.
If you say that too, that should give you the last item in that list.
So come back here.
Refresh this and I see three.
If you say something that's out of bounds like that 12 and save that change and then bring that up here
and refresh you notice.
Ill attempt to keep displaying the page.
They actually won't see anything here because technically that doesn't exist.
So instead of completely breaking on your website, what is kind of nice is Django just says, Hey,
sorry, but that thing doesn't exist.
I'm just going to leave it blank.
There is no 12th item in this list.
OK.
So again, the dot notation seems a little strange if you're used to it in Python.
But again, we're doing this in HTML, so it's pretty cool that we at least have that indexing capability.
So that's what some list.
Now let's think about some dictionary.
It's going to be extremely similar.
If I go ahead and pass in some dictionary that would pass in that entire dictionary insight, key insight
value.
But clearly, I'd probably just want the inside value.
So the way that works is you just reference it with the key.
So we say some dictionary here that.
Inside key.
Saved that changed and using that dot notation.
Refresh and now I see the inside value.
You don't really want to overdo this too much because you don't want to have in Python here a dictionary
within a dictionary and a dictionary and so on.
Typically, what's the best way to deal with these sort of simple context dictionaries is to just have
everything available on a simple, key value pair level.
That way, you don't need to worry about having to remember the dictionary variables within each other
again, or you will make this more and more complex as it goes on.
But this is just kind of the simplest way to go about it right now.
The last thing I want to show you is technically the Django template variable or language has its own
capabilities for comments.
If you wanted to comment something, what you would do is kind of put it in to hear.
And sets of curly braces, instead of having two sets of curly braces, you would say curly brace.
Pound sign or hash tag sign and then hash tag sign.
Curly braces.
And now this can be anything you want.
So this is a comment.
Go ahead and save that, and then you technically won't really see anything show up in the HTML, but
you won't get an error either.
So this is the way you put a comment in in regards to the Django template language.
If you want kind of the same thing as just doing a comment with each HTML, it's kind of up to you which
you prefer.
I usually prefer having the comments and HTML markup, but it's really up to you, whether you prefer
it to be a comment like this.
This is really helpful when you start dealing with more jingle filters and jingle text.
Actually, have the comments be associated with something more Django ish?
But again, it's kind of up to you.
OK, so that's the very basics of passing information from Python views to the template HTML thing.
Keep in mind right now is you're pretty much just create a context dictionary grab based off the key.
And then main thing to know is with variables, you're using two sets of curly braces later on.
This will change when we switch to using tags.
Attacks are more complex functionality than just sending a simple variable back.
The other thing to note is if you have a list or dictionary, you want to reference something inside
of it.
You use this dot notation and then either put in the number for the index or the key for the key value
pair.
All right.
That's it for this lecture.
I'll see you at the next one where we show you very quickly some extensions that allow you to actually
highlight this instead of having everything the same color.
I'll see you there.