› Forums › Web Development › HarvardX: CS50W – CS50’s Web Programming with Python and JavaScript › CS50W – Lecture 3 – Django › Understanding Dynamic Variables in Django Templates
- This topic is empty.
-
AuthorPosts
-
May 27, 2026 at 9:26 am #6653
A beginner learning Django often encounters code like this:
return render(request, "index.html", { "entries": util.list_entries() })At first glance, this can create several confusing questions:
- Is
entriesresponsible for the whole webpage? - Does every webpage only have one variable?
- What is the difference between static and dynamic content?
- Can one webpage have multiple variables?
These are important questions because they help explain how Django connects Python code with HTML templates.
What Exactly is
entries?In this code:
{ "entries": util.list_entries() }entriesis simply:- a variable name
- defined by the programmer
- used to send data into the HTML template
It is not a special Django keyword.
You could rename it:
{ "articles": util.list_entries() }and then use:
{{ articles }}inside the template.
One Webpage Can Have Multiple Variables
A Django webpage is not limited to only one variable.
Example:
return render(request, "index.html", { "entries": util.list_entries(), "username": "Rohan", "year": 2026, "logged_in": True, "articles_count": 15 })Now the template receives five separate variables:
entriesusernameyearlogged_inarticles_count
Using the Variables Inside the Template
The HTML template can display them like this:
<h1>Welcome {{ username }}</h1> <p>Year: {{ year }}</p> <p>Total Articles: {{ articles_count }}</p> {% if logged_in %} <p>You are logged in.</p> {% endif %} <ul> {% for entry in entries %} <li>{{ entry }}</li> {% endfor %} </ul>Each variable supplies a different piece of information to the same webpage.
Dynamic Content vs Static Content
Dynamic Content
Dynamic content comes from Python.
Examples:
{{ username }} {{ year }} {% for entry in entries %}These values can change depending on:
- database data
- logged-in user
- search results
- time
- API responses
Static Content
Static content is written directly inside the HTML template.
Example:
<h1>My Encyclopedia</h1> <footer>Powered by Django</footer>This content stays the same unless the developer edits the HTML file.
A Real Django Page Usually Mixes Both
Example:
<h1>My Encyclopedia</h1> <p>Welcome {{ username }}</p> <ul> {% for entry in entries %} <li>{{ entry }}</li> {% endfor %} </ul> <footer>Powered by Django</footer>
Static Parts
<h1>My Encyclopedia</h1> <footer>Powered by Django</footer>
Dynamic Parts
{{ username }} {% for entry in entries %}
Important Beginner Insight
The context dictionary:
{ "entries": ..., "username": ..., "year": ... }acts like a package of dynamic data sent from Python to HTML.
Each key becomes a template variable.
So:
"username"→{{ username }}"entries"→{{ entries }}"year"→{{ year }}
inside the template.
Simple Analogy
Think of the HTML template as a form with blank spaces:
Welcome ______ Year: ______ Articles: ______The Django context dictionary fills those blanks with Python data before the final webpage is shown in the browser.
- Is
-
AuthorPosts
- You must be logged in to reply to this topic.
