Menu
CoddyTech

A Page for Every Drink

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

The café's drinks now sit in DRINKS, a dictionary in menu/views.py. Its keys, like flat-white, are slugs: lowercase words joined by hyphens, made to sit in a URL.

The slug converter matches letters, digits, hyphens and underscores. Here it gives every member of staff a page, looked up in a dictionary:

path("team/<slug:handle>/", views.team_member),


def team_member(request, handle):
    person = STAFF[handle]
    return HttpResponse(f"<h2>{person['role']}</h2>")

For /team/maya-cohen/, the view gets handle="maya-cohen" and uses it as the key. One route and one view serve the whole dictionary: a new entry gets its page with no new code.

challenge icon

Challenge

Easy

Give every drink in DRINKS a page at /drinks/ plus its key:

  • one route that matches /drinks/latte/, /drinks/flat-white/ and every other key;
  • a view drink that shows the drink's name in an <h1>, then its description in a <p>.

Press Run: the preview opens /drinks/latte/.

Try it yourself

from django.http import HttpResponse

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


def home(request):
    return HttpResponse("<h1>Welcome to Corner Café</h1><p>Fresh coffee and cake every day.</p>")


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>")
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