› Forums › Web Development › HarvardX: CS50W – CS50’s Web Programming with Python and JavaScript › CS50W – Lecture 3 – Django › Examples of How Variables from views.py Are Used in Django Templates
- This topic is empty.
-
AuthorPosts
-
June 5, 2026 at 10:32 am #6795
When learning Django, many beginners understand that variables can be passed from a view to a template, but they often wonder:
If a variable such as
entriesis defined inviews.py, how is it actually used inside the HTML template?The answer is that every key in the context dictionary becomes a variable available to the template.
Passing Variables from a View
Consider the following view:
def index(request): return render(request, "index.html", { "username": "Rajeev", "year": 2026, "logged_in": True, "entries": ["Python", "Django", "HTML"] })The context dictionary contains four variables:
usernameyearlogged_inentries
These variables become available inside the template.
Displaying a String Variable
Suppose the view contains:
"username": "Rajeev"The template can display it using:
<h1>Welcome {{ username }}</h1>Django generates:
<h1>Welcome Rajeev</h1>Displaying a Numeric Variable
Suppose the view contains:
"year": 2026The template can display it using:
<p>Current Year: {{ year }}</p>Output:
<p>Current Year: 2026</p>Using a Boolean Variable with Conditions
Suppose the view contains:
"logged_in": TrueThe template can use an
ifstatement:{% if logged_in %} <p>You are logged in.</p> {% else %} <p>Please log in.</p> {% endif %}Generated output:
<p>You are logged in.</p>Using a List Variable with a Loop
Suppose the view contains:
"entries": ["Python", "Django", "HTML"]The template can loop through the list:
<ul> {% for entry in entries %} <li>{{ entry }}</li> {% endfor %} </ul>Generated output:
<ul> <li>Python</li> <li>Django</li> <li>HTML</li> </ul>Using Multiple Variables Together
A template can use several variables at the same time:
<h1>Welcome {{ username }}</h1> <p>Year: {{ year }}</p> {% if logged_in %} <p>You are logged in.</p> {% endif %} <ul> {% for entry in entries %} <li>{{ entry }}</li> {% endfor %} </ul>This combines string variables, numeric variables, boolean variables, and lists in a single page.
The Complete Flow
When a visitor requests a page, Django processes the data as follows:
Browser Request ↓ View Function ↓ Create Context Dictionary ↓ { "username": "Rajeev", "year": 2026, "logged_in": True, "entries": [...] } ↓ render(...) ↓ Template Receives Variables ↓ Generate HTML ↓ BrowserImportant Rule
The variable name used in the template must match the key used in the context dictionary.
For example:
{ "username": "Rajeev" }can be accessed using:
{{ username }}However:
{{ user }}will not work because no variable named
userwas sent from the view.Label and Package Analogy
Think of the context dictionary as a collection of labeled packages:
"username"→ Package containing “Rajeev”"year"→ Package containing 2026"logged_in"→ Package containing True"entries"→ Package containing a list of topics
The template opens each package by referring to its label:
{{ username }} {{ year }} {{ entries }}Key Takeaway
Every key in the context dictionary becomes a template variable.
Examples:
Context Dictionary Template Usage "username": "Rajeev"{{ username }}"year": 2026{{ year }}"logged_in": True{% if logged_in %}"entries": [...]{% for entry in entries %}This is the fundamental mechanism Django uses to pass dynamic data from Python code to HTML templates.
-
AuthorPosts
- You must be logged in to reply to this topic.
