StaffCoder
Django
DjangoDjango 5.2Python 3.13
18 modules · 72 challengesSign up
Module 4 · Templates

Rendered Product Page

easy100%3 of 3 solved

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:

nameskupricereleased_onmanufacturerdescription
Laptop StandLS-10045.002024-03-05DeskwiseLightweight aluminium stand that lifts any laptop to eye level and folds flat for travel in seconds
Wireless MouseWM-22025.502023-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 idText
product-name (a heading)the name
product-skuSKU: followed by the sku
product-pricePrice: $ followed by the price with exactly two decimal places
product-releasedReleased: followed by the formatted release date
product-manufacturerManufacturer: followed by the manufacturer, or Not specified
product-descriptionthe 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 &#8230;</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.

More in Templates

See the module
Conditional Stock ListingEvery product row in the listing looks the same. Number the rows, alternate their styling, show a sold-out or low-stock badge from a reusable partial, and show a friendly message when the list is empty.100%3 of 3 solvedeasySite Skeleton InheritanceEvery page would need its own copy of the HTML skeleton and navbar. Build a base template with title and content blocks and an included navbar whose links come from URL names, then have each page extend it.100%3 of 3 solvedeasyStorefront PagesA storefront needs its list and detail pages built on a shared layout. Link each product row to its detail page, reuse one stock badge on both pages, format prices to two decimals, and show a teaser in the list and the full description on the detail page.100%3 of 3 solvedeasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.