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

Article Routing

easy100%3 of 3 solved

Problem statement

A small blog has three article views ready in app/views.py (by id, by slug and by year), but app/urls.py only routes the home page, so no article URL works yet. Route the three article URLs in app/urls.py so that each view receives an already-converted, typed argument and malformed URLs are turned away by the URL resolver before any view runs.

Sample Data

This task needs no database rows. Each provided article view echoes the argument it received, so the routing result is visible in the response:

ViewArgumentResponse
article_detailarticle_id{"view": "article_detail", "value": <article_id>, "type": "<python type name>"}
article_by_slugslug{"view": "article_by_slug", "value": <slug>, "type": "<python type name>"}
article_archiveyear{"view": "article_archive", "value": <year>, "type": "<python type name>"}

Requirements

Only app/urls.py needs to change (a custom converter may live in app/urls.py or in a new module of the app package). Keep the provided home route and add these three routes:

PathNameViewKeyword argument
articles/<id>/article-detailviews.article_detailarticle_id (int)
articles/by-slug/<slug>/article-by-slugviews.article_by_slugslug (str)
articles/archive/<year>/article-archiveviews.article_archiveyear (int)

1. Each route dispatches to its own view with a typed argument

  • The URL layer does the conversion, not the view: the view must receive the already-converted value (article_id and year as int, slug as str). articles/007/ reaches article_detail with 7; articles/by-slug/2024/ reaches article_by_slug with the string "2024".
  • Each of the three routes is mapped to its own view, and each route carries the name shown above so it can be built with reverse().

2. Malformed ids and slugs never reach a view

  • The id segment accepts only non-negative integers written as plain digits: abc, 4.2, 12abc, -3 or +3 match no route.
  • The slug segment accepts only a valid slug (ASCII letters, digits, - and _): bad.slug, bad!slug, bad~slug or an empty slug match no route.
  • A URL that matches no route is rejected by the URL resolver with 404 before any view runs. Do not add catch-all routes or parse arguments inside the views.

3. The year route matches only the years 1000 to 9999

  • A year segment is exactly four digits and does not start with 0, so the accepted years are 1000 through 9999. The view receives it as an int, and reverse() turns an int year back into the same four-digit segment.
  • 1000, 2024 and 9999 match. 0999, 0000, 999, 24, 7, 20245, 10000, abcd, 20a4 and -202 do not, and return 404 from the URL resolver.
  • None of Django's built-in path converters express this rule, so you will need a custom one.
Sample Requests & Responses

GET /articles/42/

{"view": "article_detail", "value": 42, "type": "int"}

GET /articles/by-slug/hello-django/

{"view": "article_by_slug", "value": "hello-django", "type": "str"}

GET /articles/archive/2024/

{"view": "article_archive", "value": 2024, "type": "int"}

GET /articles/abc/, GET /articles/by-slug/bad.slug/, GET /articles/archive/999/, GET /articles/archive/0999/ and GET /articles/archive/20245/ all return 404 from the URL resolver; no view is called.

More in URLs & Views

See the module
Named 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 solvedeasyRecipe Site ViewsA recipe site needs its whole request layer built. Add namespaced routes for the list, detail, year archive, JSON API and a legacy redirect, with typed URL arguments, 404s for bad input, and every URL built from its route name.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.