Menu
CoddyTech

Template Variables

Part of the Django Basics section of Coddy's Django journey. Lesson 12 of 23.

The home page should name today's special, and the special lives in Python, in DRINKS. The view hands data to a template through the third argument of render(), the context: a dictionary of names the template can use.

def team(request):
    barista = {"name": "Maya", "since": 2024}
    return render(request, "menu/team.html", {"barista": barista})
{{ barista }} prints a value. A dot reaches inside it: {{ barista.name }} reads a dictionary's key or an object's attribute.
<p>{{ barista.name }} has made your coffee since {{ barista.since }}.</p>

Templates are not Python: barista["name"] is an error, and a name the context does not have prints nothing, without an error. When a value is missing from the page, check its spelling first.

challenge icon

Challenge

Easy
  • Make the home view pass the flat white from DRINKS to its template as special.
  • In home.html, after the first paragraph, add <p>Today's special: Flat White</p>, with the name read from the context.

Press Run: the home page names today's special.

Try it yourself

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Corner Café</title>
</head>
<body>
    <h1>Welcome to Corner Café</h1>
    <p>Fresh coffee and cake every day.</p>
    <a href="{% url 'menu:about' %}">About us</a>
</body>
</html>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Django Basics

Practice on your own: Django playground