Locked lesson.
About this lesson
In this lesson, we'll discuss the purpose of the Context Dictionary and how it works with the variables you might use within your project.
Exercise files
Download this lesson’s related exercise files.
Context Dictionary.docx57.4 KB Context Dictionary - Solution.docx
57.9 KB
Quick reference
Context Dictionary
A context dictionary allows you to pass variables or any type of data from your Django backend to a web page.
When to use
Use them any time you have data on your back end (in views.py or anywhere else) that you want to pass to a web page and use on that web page.
Instructions
Context dictionaries are python dictionaries. They're composed of key/value pairs that can be passed to a web page and then accessed using a Django Template Language variable.
Hints & tips
- Context Dictionaries go in the page function of your views.py file.
- {'first_name': "John"}
- {{ first_name }}
- 00:04 Okay, in this video, I want to talk about the context of dictionary.
- 00:07 So if we look at the Django documentation, our template language page that we've been
- 00:11 looking at, if we scroll down a little bit, we could see this variables and
- 00:15 we can use this Django template language to pass variables onto our HTML pages
- 00:20 just by using double curly brackets like this, and then whatever variable is.
- 00:24 So this ties in with the context dictionary.
- 00:26 So let's head back over to our code, and go to your views.pyfile.
- 00:31 And remember when we defined each page, we said what template it is, and
- 00:35 then we put these two curly brackets, and I didn't really talk about what those are.
- 00:40 And these are the context dictionary.
- 00:43 So we can use Python dictionaries to pass stuff from our back end to our front end,
- 00:48 and then use them as variables on our page.
- 00:50 So let's just build one real quick and I'll walk you through it here.
- 00:54 So in our about page, let's create one called first_name,
- 01:01 and then we put a colon and then double quotation marks, John.
- 01:08 So this is a Python dictionary, right?
- 01:10 Python, dictionaries have key value pairs.
- 01:14 So this is the key, this is the value.
- 01:17 And we can reference this just by calling the key.
- 01:20 So we could call first name and it will output, John, right?
- 01:23 The keys and the values are separated by a colon.
- 01:27 So we can now use this on our, about page.
- 01:30 It won't work for our homepage, it only works on the page where the dictionary is
- 01:34 sitting here on your views.pyfile.
- 01:36 So on our about page, we can pull this up, about that HTML,
- 01:40 and let's add a couple of line breaks here just to move this down the screen.
- 01:44 And to use these variables on the page,
- 01:47 we just use double curly brackets and then just curl the variable or
- 01:52 in our case, the context key which was first_name.
- 01:56 And I'm going to put a little dash in front of it, so
- 01:58 like I'm signing John to the page.
- 02:00 Now if we save this, and head back over to our about page and hit reload,
- 02:05 boom, we see there it is, John.
- 02:07 So very cool.
- 02:09 Now, I've done this sort of manually right here, but
- 02:13 we can separate this out into anything we want.
- 02:16 So let's say we want to create a regular variable,
- 02:19 let's call it first name, and we can set that equal to John.
- 02:23 Now instead of typing John here, we can just use this variable.
- 02:29 Let's change this around, let's call it f_name, just so
- 02:33 it looks a little bit different than this one, so it's not quite as confusing.
- 02:38 So we could just copy this and instead of John here in quotation marks,
- 02:42 we can just put that variable name.
- 02:44 So now this variable, John, will get passed in here and
- 02:48 will be reassigned to the name, first name, save this.
- 02:52 This stays the same because we're still calling in that same context variable.
- 02:58 So if we save this, come back to our page and hit reload,it stays the same or
- 03:03 we can make sure this worked correctly by going to our views.pyfile and let's
- 03:08 just change this to Bob, save this, head back over to our webpage and hit reload.
- 03:14 Boom, now it says Bob.
- 03:15 So, very, very cool and pretty easy to use.
- 03:18 Now, we've just done one right here, you can do as many as you want.
- 03:21 You just separate them with a comma.
- 03:24 Let's make another one.
- 03:25 Last_name, colon,
- 03:29 double quotation marks, Elder.
- 03:34 So we save this, head back over to our about page, and
- 03:38 let's put another one here.
- 03:40 Let's go last underscore name.
- 03:43 Save this, And back here and hit reload.
- 03:48 Now says Bob Elder, right?
- 03:51 If we didn't want to change that from Bob back to John, we can do that.
- 03:56 So what happens if you put single quotes here?
- 04:01 Save this, come back here and hit reload.
- 04:05 It still works.
- 04:06 I like to use double quotes,
- 04:09 just to change this around, just so it's easier for
- 04:15 me to notice that this is the key, this is the value.
- 04:21 I know my keys are in single quotes and my values are in double quotes.
- 04:24 That's just what I do personally.
- 04:25 You absolutely don't have to do that.
- 04:27 For instance, like right here, we're just using a variable.
- 04:32 And we can do the same thing here.
- 04:34 We can go L_name equals Elder.
- 04:37 Let's put Elders just so we make sure that it's changed.
- 04:42 And here we can just call L_name.
- 04:44 Save this, and
- 04:45 again, no quotation marks at all because we're referencing this variable.
- 04:50 All right, so if we save this, come back to our page and hit reload.
- 04:53 This should say John Elders, and it does, John Elders.
- 04:57 I'm going to go ahead and change this back because my name is not Elders.
- 05:01 So very cool.
- 05:02 You can pass in anything you want.
- 05:03 We're here we're passing strings, strings of text.
- 05:08 You could pass in numbers if you want, right, 41, without quotation marks.
- 05:13 Whatever you like, any sort of Python data types that you can think of,
- 05:17 you can pass through there through your context variable.
- 05:21 Very, very cool.
- 05:22 So that's all for context dictionaries and variables,
- 05:28 in the next video, we'll look at filters.
Lesson notes are only available for subscribers.