› Forums › Web Development › HarvardX: CS50W – CS50’s Web Programming with Python and JavaScript › CS50W – Lecture 3 – Django › Understanding a Django Template That Displays All Encyclopedia Pages
- This topic is empty.
-
AuthorPosts
-
April 19, 2026 at 10:46 am #6420
When building websites with Django, templates are responsible for showing content to visitors. They allow developers to combine HTML with dynamic data coming from Python views.
This example is useful for beginners because it demonstrates:
Template inheritance Named content blocks Dynamic page titles Looping through data Displaying a list of pages
Template Code
{% extends "encyclopedia/layout.html" %} {% block title %} Encyclopedia {% endblock %} {% block body %} All Pages {% for entry in entries %} {{ entry }} {% endfor %} {% endblock %}
What This Template Does
This template creates a webpage that shows a list of encyclopedia entries.
When a visitor opens the page:
Django loads a shared layout file The page title becomes Encyclopedia The heading All Pages is displayed Every item inside entries is printed on the pageIf the list contains:
["Python", "HTML", "CSS", "Django"]The visitor may see:
All Pages Python HTML CSS Django
Line-by-Line Explanation
1. Extending a Base Template
{% extends "encyclopedia/layout.html" %}This tells Django to use another template as the main structure of the page.
That parent template often contains:
Navigation menu Search bar Header Footer Shared stylingInstead of repeating full HTML on every page, developers reuse one layout.
This is called template inheritance.
2. Title Block
{% block title %} Encyclopedia {% endblock %}This fills the title area defined in the parent template.
If the layout contains:
<title>{% block title %}{% endblock %}</title>Then the browser tab will show:
Encyclopedia
3. Body Block
{% block body %}This begins the main content area of the page.
Everything inside this block becomes the visible page content.
4. Static Text Heading
All PagesThis text is shown directly to visitors.
It acts as a heading or label for the list below.
5. Looping Through Entries
{% for entry in entries %}This tells Django:
Repeat the next section once for each item in the entries list.If entries contains four items, the loop runs four times.
6. Displaying Each Entry
{{ entry }}This prints the current item from the loop.
Example output:
Python HTML CSS Django
7. Ending the Loop
{% endfor %}This closes the loop.
8. Ending the Body Block
{% endblock %}This closes the page body section.
Where entries Comes From
The entries variable usually comes from a Django view such as:
def index(request): return render(request, "encyclopedia/index.html", { "entries": ["Python", "HTML", "CSS"] })Meaning:
View sends data Template displays data
What Visitors Actually See
If the backend sends:
["Python", "SQL", "JavaScript"]The webpage may display:
All Pages Python SQL JavaScript
Key Django Syntax Beginners Should Know
Template Tags
Used for logic:
{% for %} {% block %} {% extends %}
Template Variables
Used for displaying values:
{{ entry }}
Why This Matters in Real Projects
Using templates allows developers to:
Reuse layouts across many pages Keep code organized Separate design from backend logic Display dynamic database content Build scalable websites fasterThis is one reason Django remains popular for web development.
Beginner Summary
Use a shared layout Set the page title Print every encyclopedia page dynamicallyIt is a small file, but it introduces some of the most important concepts in Django development.
Golden Rule of Django
Views prepare the data Templates present the data -
AuthorPosts
- You must be logged in to reply to this topic.
