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

Conditional Stock Listing

easy100%3 of 3 solved

Problem statement

The Product model and the GET /products/ view are provided, but the listing template only has a heading and a placeholder, so no products are shown. Complete the listing with Django template tags: loop over the products, number and stripe the rows by their position, show a stock badge that depends on how many units are left (the badge lives in its own small partial template), and show a message when there are no products at all.

Sample Data

The provided Product model (app/models.py) has name (CharField) and stock (PositiveIntegerField, default 0), with Meta.ordering = ["name"]. Example rows:

namestock
Anchor Bolt12
Brass Hinge0
Cedar Plank1
Drill Bit0
Epoxy Resin7

Requirements

Change only the two templates: app/templates/app/product_list.html and app/templates/app/stock_badge.html. The provided product_list view (GET /products/, URL name product-list) renders app/product_list.html with one context variable, products: every product, already ordered by name. Do not change the view. The provided <style> block already styles the classes below.

1. One row per product, in order

  • Render exactly one row element per product, in the order the view supplies them. Each row element has the class product-row.
  • Inside each row, an element with the class product-name holds the product's name, and an element with the class row-number holds the row's position: 1 for the first row, 2 for the second, and so on.
  • A list suits the rows well: a <ul class="product-list"> with one <li> per row matches the provided styles.

2. Striped rows

  • Each row has exactly one parity class in addition to product-row: row-odd for the 1st, 3rd, 5th, ... row and row-even for the 2nd, 4th, 6th, ... row.
  • Parity and row number depend only on the row's position in the listing, never on the product's id or stock.

3. Stock badge partial - app/stock_badge.html

  • The listing includes this partial once inside every row, whatever the stock. The partial reads the current row's product and decides what to show:
    • 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 1 left)
    • stock is 4 or more: nothing
  • The stock condition lives inside the partial, not in the listing, so editing the partial alone changes the badge on every row. No badge appears outside a row.

4. Empty state

  • When products is empty, render one element with the class empty-state and the text No products available., and no rows.
  • The empty-state element must not sit inside a <ul> or <ol> (a list may only contain <li> items). If you wrap the rows in a list, render the message instead of the list when there are no products, not inside it.
  • When there is at least one product, the empty-state element is not rendered.

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.

Sample Requests & Responses

GET /products/ with the sample data (whitespace may differ):

<ul class="product-list">
  <li class="product-row row-odd"><span class="row-number">1</span> <span class="product-name">Anchor Bolt</span></li>
  <li class="product-row row-even"><span class="row-number">2</span> <span class="product-name">Brass Hinge</span> <span class="badge-sold-out">Sold out</span></li>
  <li class="product-row row-odd"><span class="row-number">3</span> <span class="product-name">Cedar Plank</span> <span class="badge-low-stock">Only 1 left</span></li>
  <li class="product-row row-even"><span class="row-number">4</span> <span class="product-name">Drill Bit</span> <span class="badge-sold-out">Sold out</span></li>
  <li class="product-row row-odd"><span class="row-number">5</span> <span class="product-name">Epoxy Resin</span></li>
</ul>

GET /products/ with no products (no <ul> at all):

<p class="empty-state">No products available.</p>

More in Templates

See the module
Rendered Product PageThe product page has no template yet. Render the product from a template with a readable release date, a fallback for a missing manufacturer, a short description teaser and prices that always show two decimals.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.