Menu
CoddyTech

The Full Menu

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

The café's full menu is in MENU in menu/views.py: a dictionary of sections, each a list of items. To loop over a dictionary's keys and values together, call its items method. A template calls a method without parentheses, and a loop can unpack two values, like Python's for key, value in d.items(). A week of daily specials:

{% for day, specials in week.items %}
    <h3>{{ day }}</h3>
    <p>{{ specials|join:", " }}</p>
{% endfor %}

Each value is a list, so it can be looped over in turn: a {% for %} inside a {% for %}, each closed by its own {% endfor %}.

challenge icon

Challenge

Medium
menu/menu.html is started for you. Add the menu page at /menu/:
  • a view that renders it with MENU, and a route named menu;
  • one <section class="menu-section"> per section, its name in an <h2>;
  • one <li class="menu-item"> per item: its name, then <span class="price">$4.00</span>.

Press Run: the preview opens /menu/.

Try it yourself

from django.http import Http404, HttpResponse
from django.shortcuts import render

DRINKS = {
    "espresso": {
        "slug": "espresso",
        "name": "Espresso",
        "price": 2.5,
        "description": "A short, strong shot of coffee.",
        "sold_out": False,
    },
    "latte": {
        "slug": "latte",
        "name": "Latte",
        "price": 3.5,
        "description": "Espresso with lots of steamed milk.",
        "sold_out": False,
    },
    "flat-white": {
        "slug": "flat-white",
        "name": "Flat White",
        "price": 3.75,
        "description": "Espresso with a thin layer of silky milk.",
        "sold_out": False,
    },
    "hot-chocolate": {
        "slug": "hot-chocolate",
        "name": "Hot Chocolate",
        "price": 3,
        "description": "Dark chocolate melted into warm milk.",
        "sold_out": True,
    },
}


MENU = {
    "Coffee": [
        {"name": "Espresso", "price": 2.5},
        {"name": "Latte", "price": 3.5},
        {"name": "Flat White", "price": 3.75},
    ],
    "Tea": [
        {"name": "Green Tea", "price": 2.5},
        {"name": "Chai", "price": 3.25},
    ],
    "Cakes": [
        {"name": "Carrot Cake", "price": 4},
        {"name": "Lemon Tart", "price": 3.5},
    ],
}


def home(request):
    return render(request, "menu/home.html", {"special": DRINKS["flat-white"]})


def drink_list(request):
    return render(request, "menu/drinks.html", {"drinks": DRINKS.values()})


def about(request):
    return HttpResponse("Corner Café opened in 2026.")


def hours(request):
    return HttpResponse("Open every day from 8:00 to 18:00.")


def contact(request):
    return HttpResponse("Write to us at hello@cornercafe.example.")


def table(request, number):
    return HttpResponse(f"<h1>Table {number}</h1>")


def drink(request, slug):
    if slug not in DRINKS:
        raise Http404(f"No drink called {slug}")
    item = DRINKS[slug]
    return HttpResponse(f"<h1>{item['name']}</h1><p>{item['description']}</p>")


def order(request):
    size = request.GET.get("size", "medium")
    if size not in ["small", "medium", "large"]:
        size = "medium"
    return HttpResponse(f"You ordered a {size} coffee.")

All lessons in Django Basics

Practice on your own: Django playground