Problem statement
The storefront has a Product model and two routes, a product listing and a product detail page, but the views are stubs and all five templates are empty placeholders. Build the page layer so both pages share one base template and navbar, the listing loops over real data with striped, numbered rows that link to each product's page, both pages share one stock-badge partial, filters format prices, dates and text, and an empty catalog shows a clear message. This problem combines the skills from the earlier template problems; the few new pieces are listed under "New in this problem".
Provided
The Product model (app/models.py), ordered by name (Meta.ordering = ["name"]):
| Field | Type |
|---|---|
| name | CharField(max_length=100) |
| sku | CharField(max_length=20, unique=True) |
| price | DecimalField(max_digits=8, decimal_places=2) |
| released_on | DateField |
| manufacturer | CharField(max_length=100, blank=True) - may be empty |
| description | TextField |
| stock | PositiveIntegerField(default=0) |
| Route | URL name | View | Renders |
|---|---|---|---|
| GET /products/ | product-list | views.product_list | app/product_list.html |
| GET /products/<int:pk>/ | product-detail | views.product_detail | app/product_detail.html |
Sample data:
| name | sku | price | released_on | manufacturer | stock |
|---|---|---|---|---|---|
| Laptop Stand | LS-100 | 45.00 | 2024-03-05 | Deskwise | 12 |
| Wireless Mouse | WM-220 | 25.50 | 2023-12-25 | (empty) | 0 |
| Desk Lamp | DL-7 | 19.90 | 2022-07-01 | Brightside | 2 |
Requirements
Change only app/views.py and the five templates in app/templates/app/ (each template currently holds a {# Write your code here #} placeholder). Every value a page shows must come from the context the view passes; no product value may be typed into a template.
1. Views - app/views.py
- product_list: render app/product_list.html with one context variable, products: every product in the model's default order (by name).
- product_detail: render app/product_detail.html with one context variable, product: the Product instance with the requested pk.
- Keep formatting out of the views: pass the model data as-is and let the templates format it.
2. Base template - app/base.html (the shared chrome)
- Provides the whole HTML page exactly once: the output starts with <!DOCTYPE html> and has one <html lang="en">, one <head>, one <title>, one <body> and one <main>.
- Defines a title block (the text of <title>, default Storefront) and a content block (everything inside <main>, default a single paragraph reading Content coming soon.). A template that extends the base without overriding a block gets that block's default.
- Includes app/navbar.html once, outside <main>.
3. Navbar partial - app/navbar.html
- Only the navbar: an element with the id site-nav holding one link, All products, to the product-list URL.
- The base includes it; the pages never include or repeat it, so editing this one file changes every page.
4. Stock badge partial - app/stock_badge.html
- Reads product from the context and renders at most one badge:
- stock is 0: an element with the class badge-sold-out and the text Sold out
- stock is 1, 2 or 3: an element with the class badge-low-stock and the text Only N left, where N is the stock (for example Only 2 left)
- stock is 4 or more: nothing
- Both pages include this partial unconditionally (once per listing row, once on the detail page) and keep the stock condition inside it, so editing this one file changes the badges on both pages.
5. List page - app/product_list.html
- Extends app/base.html and only overrides blocks. The title is All Products; the content starts with an <h1> reading All Products.
- Exactly one row element per product, in the order given. Each row has the class product-row plus exactly one parity class by position: row-odd for the 1st, 3rd, 5th, ... row and row-even for the 2nd, 4th, ... row (position only, never id or stock).
- Inside each row, one element of each:
- class row-number: the row's position (1, 2, ...)
- class product-name: the name, as a link (<a>) to that product's detail page. The link is the only link in the row, its text is the name, and its target is built from the URL name product-detail and the product's pk (not a typed path, so the links still work if the routes move).
- class product-price: $ followed by the price with exactly two decimal places ($45.00, and $7.50 for Decimal("7.5"))
- the stock badge partial
- class product-teaser: the description cut to ten words (ten words or fewer are shown in full; longer ones keep the first ten words followed by a space and the ellipsis character U+2026)
- When products is empty, render one element with the class empty-state and the text No products available. inside the content block, and no rows. When there is at least one product, the empty-state element is not rendered.
6. Detail page - app/product_detail.html
- Extends app/base.html and only overrides blocks. The title is the product name. The content holds these elements (each id once) plus the stock badge partial:
| Element id | Text |
|---|---|
| product-name (a heading) | the name |
| product-sku | SKU: + sku |
| product-price | Price: $ + the price with exactly two decimal places |
| product-released | Released: + the release date as full month name, day without a leading zero, four-digit year (Released: March 5, 2024) |
| product-manufacturer | Manufacturer: + the manufacturer, or Not specified when it is empty, None or missing |
| product-description | the full description, not truncated |
7. Plain-dictionary contexts
- The templates must also work with plain dictionaries in place of model instances: product as a dict with the model's field names plus pk and id (both the same number, as on a model instance), and products as a list of such dicts. Prices given this way may have fewer decimal places (Decimal("7.5"), 12), which is why the price needs a formatting filter even though database prices already look like 45.00.
How markup is matched. Each element is found by its id or class, and only its text counts, with runs of whitespace collapsed to one space. Extra attributes and classes, wrapper elements, and tags inside an element (such as <strong>) are all fine; only the element's text must match exactly.
New in this problem
- Links with an argument. Each listing row links to its product's detail page; the link target needs the URL name plus the product's pk. Earlier problems only linked to URLs without arguments.
- One partial on two pages. The stock badge partial is included from both the listing loop and the detail page.
- One field, two presentations. The listing shows a ten-word teaser of the description while the detail page shows it in full.
Sample Requests & Responses
GET /products/ with the sample data returns 200 (whitespace may differ):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>All Products</title>
</head>
<body>
<nav id="site-nav"><a href="/products/">All products</a></nav>
<main>
<h1>All Products</h1>
<section class="product-list">
<article class="product-row row-odd">
<span class="row-number">1</span>
<span class="product-name"><a href="/products/3/">Desk Lamp</a></span>
<span class="product-price">$19.90</span>
<span class="badge-low-stock">Only 2 left</span>
<p class="product-teaser">Warm light for late nights.</p>
</article>
<article class="product-row row-even">
<span class="row-number">2</span>
<span class="product-name"><a href="/products/1/">Laptop Stand</a></span>
<span class="product-price">$45.00</span>
<p class="product-teaser">Lightweight aluminium stand that lifts any laptop to eye level …</p>
</article>
<article class="product-row row-odd">
<span class="row-number">3</span>
<span class="product-name"><a href="/products/2/">Wireless Mouse</a></span>
<span class="product-price">$25.50</span>
<span class="badge-sold-out">Sold out</span>
<p class="product-teaser">Quiet clicks and a two year battery.</p>
</article>
</section>
</main>
</body>
</html>
(… stands for the single ellipsis character appended to a truncated teaser; the sample rows have pks 1, 2 and 3 in the order they were created.)
GET /products/ with no products returns the same skeleton, navbar and title with <p class="empty-state">No products available.</p> inside <main> and no rows.
GET /products/<pk>/ for Wireless Mouse returns 200 with <title>Wireless Mouse</title> and, inside <main>:
<h1 id="product-name">Wireless Mouse</h1> <p id="product-sku">SKU: WM-220</p> <p id="product-price">Price: $25.50</p> <p id="product-released">Released: December 25, 2023</p> <p id="product-manufacturer">Manufacturer: Not specified</p> <span class="badge-sold-out">Sold out</span> <p id="product-description">Quiet clicks and a two year battery.</p>
