Thực đơn đầy đủ
Thuộc phần Kiến thức cơ bản về Django trong hành trình Django của Coddy. Bài học 21 trên 23.
Toàn bộ thực đơn của quán cà phê nằm trong MENU ở menu/views.py: một từ điển gồm các phần, mỗi phần là một danh sách món. Để lặp qua các khóa và giá trị của từ điển cùng lúc, hãy gọi phương thức items của nó. Trong template, bạn gọi phương thức mà không có dấu ngoặc đơn, còn vòng lặp có thể giải nén hai giá trị, như for key, value in d.items() của Python. Thực đơn đặc biệt mỗi ngày trong tuần:
{% for day, specials in week.items %}
<h3>{{ day }}</h3>
<p>{{ specials|join:", " }}</p>
{% endfor %}Mỗi giá trị là một danh sách, nên bạn có thể lần lượt lặp qua danh sách đó: một {% for %} bên trong một {% for %}, mỗi vòng lặp được đóng bằng {% endfor %} riêng.
Thử thách
Trung bìnhThêm trang thực đơn đầy đủ.
- Trong
menu/views.py, viết một view hiển thịmenu/menu.htmlvớiMENUlàmmenu. - Trong
menu/urls.py, thêm tuyến đườngmenu/có tênmenu. - Trong
menu.html, lặp qua các phần: mỗi phần có một<section class="menu-section">, với tên phần trong<h2>. Bên trong, lặp qua các món: mỗi món có một<li class="menu-item">, gồm tên món, rồi đến<span class="price">$4.00</span>.
Mở /menu/: trang liệt kê Coffee, Tea và Cakes, còn Carrot Cake có giá $4.00.
Tự làm thử
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.")
Tất cả bài học trong Kiến thức cơ bản về Django
Tự luyện tập: Playground Django