StaffCoder
Django
DjangoDjango 5.2Python 3.13
18 modules · 72 challengesSign up
Module 3 · URLs & Views

Recipe Site Views

easy100%3 of 3 solved

Problem statement

A recipe site needs its whole request surface built: browsable pages, a JSON API, stable named links, and graceful misses. The Recipe model and the page markup are provided, app/urls.py routes only the home page, and every view in app/views.py is a stub. Route and implement the recipe list, detail by slug, year archive, a JSON detail endpoint and a redirect for the old recipe path inside a namespaced URLconf; produce every internal link (the three templates and the JSON payload) from route names; and answer 404 for anything missing.

Sample Data

The provided Recipe model (app/models.py) has title (CharField), slug (SlugField, unique), year (PositiveIntegerField) and description (TextField, may be blank), ordered newest year first, then by title. Example rows:

idtitleslugyeardescription
1Tomato Souptomato-soup2024Roast the tomatoes first.
2Lemon Tartlemon-tart2024(blank)
3Pumpkin Breadpumpkin-bread2019A moist loaf.

Requirements

Edit app/urls.py, app/views.py and the three templates in app/templates/app/. The provided home route ("", name="home") must stay in app/urls.py; project/urls.py already includes app.urls at the site root.

1. URLconf - app/urls.py

  • Every route lives in the app namespace recipes. The route names below reverse only as recipes:<name>; a bare name such as recipe-list must not reverse.
  • Routes, with the keyword argument each view receives:
PathNameViewArgument
recipes/recipe-listrecipe_list(request)-
recipes/<slug>/recipe-detailrecipe_detail(request, slug)slug - str
recipes/archive/<year>/recipe-archiverecipe_archive(request, year)year - int
api/recipes/<slug>/recipe-jsonrecipe_json(request, slug)slug - str
recipe/<id>/legacy-recipelegacy_recipe_redirect(request, pk)pk - int
  • The URL layer converts every argument before the view runs: slug is a slug (ASCII letters, digits, -, _) and stays a string even when it is all digits (/recipes/2024/ is a recipe detail, slug "2024"); pk is a non-negative integer (/recipe/007/ passes pk=7); year is exactly four digits with no leading 0 (so 1000 to 9999, the same rule as in Article Routing) and arrives as an int.
  • Malformed URLs must 404 at routing and never reach a view: a slug containing other characters (bad.slug, bad!slug, bad~slug) or an empty slug, a non-integer or signed id (abc, 4.2, 12abc, -3, +3), and a year outside that rule (0999, 0000, 0024, 999, 24, 20245, 10000, abcd, 20a4, -202).
  • Each route maps to its own view.

2. Views - app/views.py

  • recipe_list: render app/recipe_list.html with every recipe in the context under recipes.
  • recipe_detail: render app/recipe_detail.html with the recipe whose slug matches under recipe; 404 when no recipe has that slug (never existed, or deleted).
  • recipe_archive: render app/recipe_archive.html with year (the int from the URL) and recipes (only the recipes of that year).
  • recipe_json: for a known slug return a JSON response (Content-Type: application/json) whose body is exactly these fields and nothing else; 404 for an unknown slug:
  • id - integer primary key
  • title, slug, description - strings (description is "" when blank)
  • year - integer
  • url - the recipe's detail page path, produced from the recipes:recipe-detail route name (not assembled by hand)

Do not build the JSON string by hand; values containing quotes, backslashes or newlines must still produce valid JSON. Values are read from the database on every request.

  • legacy_recipe_redirect: permanently redirect (301) the old recipe/<id>/ path to the recipe's recipes:recipe-detail URL, built by name from the recipe's current slug; 404 (not a redirect) for an unknown or deleted id.

3. Links - the three templates

  • Every href marked {# Write your code here #} must be produced from the namespaced route names inside the template - never a hardcoded path:
  • recipe_list.html: each recipe links to its detail page and to its year's archive.
  • recipe_detail.html: links to the recipe list, the archive of the recipe's year, and the recipe's JSON endpoint (in that order).
  • recipe_archive.html: each listed recipe links to its detail page; then a link to the recipe list.
  • Because every link, redirect and url field is built from route names, moving the app under another prefix (for example mounting app.urls at v2/) or renaming any path while keeping its name must keep every link, the legacy redirect and the JSON url field correct with no other change.
Sample Requests & Responses

GET /recipes/

200, HTML rendered from app/recipe_list.html; contains <a href="/recipes/tomato-soup/">Tomato Soup</a> and <a href="/recipes/archive/2024/">2024</a>.

GET /recipes/pumpkin-bread/

200, HTML rendered from app/recipe_detail.html; its links are /recipes/, /recipes/archive/2019/ and /api/recipes/pumpkin-bread/.

GET /recipes/no-such-recipe/

404

GET /recipes/archive/2024/

200, HTML rendered from app/recipe_archive.html; context year is 2024, recipes holds Tomato Soup and Lemon Tart.

GET /recipes/archive/24/

404 (not routed)

GET /api/recipes/tomato-soup/

200, Content-Type: application/json

{"id": 1, "title": "Tomato Soup", "slug": "tomato-soup", "year": 2024, "description": "Roast the tomatoes first.", "url": "/recipes/tomato-soup/"}

GET /api/recipes/no-such-recipe/

404

GET /recipe/3/

301, Location: /recipes/pumpkin-bread/

GET /recipe/999/

404

GET /recipe/abc/

404 (not routed)

More in URLs & Views

See the module
Article RoutingOnly the home page is routed, so every article URL is a 404. Route articles by id, slug and four-digit year so the view receives typed values and malformed URLs are rejected before any view runs.100%3 of 3 solvedeasyNamed Routes And RedirectsLinks and redirects break every time a URL changes. Name the routes inside an app namespace, build every link from those names, and send old post URLs to their new home with a permanent redirect.100%3 of 3 solvedeasySafe Detail And JSON ViewsProduct pages need a safe detail view and a JSON endpoint. Return the product page or a 404 for unknown ids, and serve the same product as properly encoded JSON with its price as a two-decimal string.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.