Problem statement
The shop already has a Product model and a GET /products/<pk>/ route, but the page itself has not been built: the product_page view in app/views.py is a stub and app/templates/app/product_detail.html is empty. Build the page so the view only looks up the product and hands it to a template, and the template does all the display work: it shows the product's fields and uses Django's built-in filters to format the date and the price, fall back to a default for a missing value, and shorten a long description.
Sample Data
The provided Product model (app/models.py) has name (CharField), sku (CharField, unique), price (DecimalField, 2 decimal places), released_on (DateField), manufacturer (CharField, may be blank) and description (TextField). Example rows:
| name | sku | price | released_on | manufacturer | description |
|---|---|---|---|---|---|
| Laptop Stand | LS-100 | 45.00 | 2024-03-05 | Deskwise | Lightweight aluminium stand that lifts any laptop to eye level and folds flat for travel in seconds |
| Wireless Mouse | WM-220 | 25.50 | 2023-12-25 | (blank) | Quiet clicks and a two year battery. |
Requirements
Two files need to change: app/views.py and app/templates/app/product_detail.html.
1. The view renders a template with a context
- product_page(request, pk) looks up the Product with that pk and renders app/product_detail.html with one context variable, product, whose value is the model instance itself.
- The view does no presentation work: it does not build HTML, format dates or numbers, or cut text. All of that happens in the template.
2. The template shows six elements
Everything shown comes from product in the context, never from text typed into the template.
| Element id | Text |
|---|---|
| product-name (a heading) | the name |
| product-sku | SKU: followed by the sku |
| product-price | Price: $ followed by the price with exactly two decimal places |
| product-released | Released: followed by the formatted release date |
| product-manufacturer | Manufacturer: followed by the manufacturer, or Not specified |
| product-description | the description, shortened to ten words |
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.
3. Formatting rules
- Price: always exactly two decimal places, rounded. A price stored in the database already looks like 45.00, but the template is also given values such as Decimal("7.5"), 12 or Decimal("1234.567"), which must show as 7.50, 12.00 and 1234.57.
- Release date: full month name, day without a leading zero, four-digit year (2024-03-05 becomes March 5, 2024). The raw ISO date must not appear anywhere on the page.
- Manufacturer: when it is blank, None or absent from the context, show Not specified instead.
- Description: ten words or fewer are shown in full. A longer one keeps its first ten words followed by a space and the ellipsis character U+2026, which is exactly what Django's word-truncating filter appends.
- Django's built-in template filters cover these rules.
4. Output follows the context
- The same template renders whatever product it is given: a different product, or changed values for the same product, produce different output with no change to the template.
- The template must also work when product is a plain dictionary (for example {"product": {"name": "Desk Lamp", ...}}), so build the page only from template variables and filters.
Sample Requests & Responses
GET /products/1/ (Laptop Stand)
<h1 id="product-name">Laptop Stand</h1> <p id="product-sku">SKU: LS-100</p> <p id="product-price">Price: $45.00</p> <p id="product-released">Released: March 5, 2024</p> <p id="product-manufacturer">Manufacturer: Deskwise</p> <p id="product-description">Lightweight aluminium stand that lifts any laptop to eye level …</p>
GET /products/2/ (Wireless Mouse)
<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> <p id="product-description">Quiet clicks and a two year battery.</p>
… stands for the ellipsis character that Django's word-truncating filter appends; the manufacturer is blank, so the default is shown.
