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:
| View | Argument | Response |
|---|---|---|
| article_detail | article_id | {"view": "article_detail", "value": <article_id>, "type": "<python type name>"} |
| article_by_slug | slug | {"view": "article_by_slug", "value": <slug>, "type": "<python type name>"} |
| article_archive | year | {"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:
| Path | Name | View | Keyword argument |
|---|---|---|---|
| articles/<id>/ | article-detail | views.article_detail | article_id (int) |
| articles/by-slug/<slug>/ | article-by-slug | views.article_by_slug | slug (str) |
| articles/archive/<year>/ | article-archive | views.article_archive | year (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.
