Problem statement
A small blog is about to change its URL layout, and the team wants every link to survive the move. The Post model, the routes, the post list, detail and year-archive views and the page markup are provided, but the routes have no names, every link in the three templates is still an empty placeholder, and the old /blog/<id>/ address is not implemented. Name the routes inside a blog namespace in app/urls.py, fill in the template links from those names, and implement legacy_post_redirect in app/views.py so old links land on the post's current page. No URL path may be typed by hand in a view or a template.
Sample Data
The provided Post model (app/models.py) has title (CharField), slug (SlugField) and year (PositiveIntegerField), ordered newest year first, then by title. Example rows:
| id | title | slug | year |
|---|---|---|---|
| 1 | Hello World | hello-world | 2024 |
| 2 | Routing Deep Dive | routing-deep-dive | 2024 |
| 3 | Django Tips | django-tips | 2019 |
Requirements
Change only app/urls.py, app/views.py (the legacy_post_redirect view) and the three templates under app/templates/app/. The paths in app/urls.py and the post_list, post_detail, post_archive and home views are provided and must keep working as they are.
1. Name the routes inside the blog namespace (app/urls.py)
- The app's URL namespace is blog, and each route has this name (the paths are already in the starter):
| Path | View | Namespaced name | Example URL |
|---|---|---|---|
| posts/ | post_list | blog:post-list | /posts/ |
| posts/<year>/<slug>/ | post_detail | blog:post-detail (year, slug) | /posts/2024/hello-world/ |
| archive/<year>/ | post_archive | blog:post-archive (year) | /archive/2019/ |
| blog/<id>/ | legacy_post_redirect | not required | - |
- The names work only through the namespace: blog:post-list reverses to /posts/, while a bare post-list does not reverse at all. Once the namespace is set, the provided home route becomes blog:home; that is expected.
- The year and slug can be passed either positionally or by keyword (year, slug); both give the same URL.
2. Build every template link from its route name
- Fill each {# Write your code here #} placeholder so the href is produced from the route name inside the template (not typed as a path), and renders exactly the URL that name gives for the same arguments:
- post_list.html: for each post, a link to its detail page (blog:post-detail with its year and slug) and a link to its year's archive (blog:post-archive with its year).
- post_detail.html: a link to the post list (blog:post-list) and a link to the archive of the post's year.
- post_archive.html: for each post of that year, a link to its detail page, and a link to the post list.
3. Redirect the legacy path (legacy_post_redirect in app/views.py)
- GET /blog/<id>/ is the old URL of a post. It answers with a permanent redirect (301) whose Location is the post's detail URL, produced from the blog:post-detail name with the post's current year and slug (so if a post's slug or year changes, the redirect follows). For example /blog/3/ redirects to /posts/2019/django-tips/.
- If no post has that id, the response is 404 (no redirect, no server error).
4. Paths can change, names cannot
- No URL path may be hardcoded in a view or a template. If a path is renamed (for example posts/ becomes articles/, or the whole app is mounted under a new prefix such as v2/) while the route names stay the same, every rendered link and the legacy redirect must follow the new paths without any other change.
Sample Requests & Responses
GET /posts/ (links rendered in the page)
<a href="/posts/2024/hello-world/">Hello World</a> (<a href="/archive/2024/">2024</a>) <a href="/posts/2024/routing-deep-dive/">Routing Deep Dive</a> (<a href="/archive/2024/">2024</a>) <a href="/posts/2019/django-tips/">Django Tips</a> (<a href="/archive/2019/">2019</a>)
GET /posts/2019/django-tips/
<a href="/posts/">All posts</a> <a href="/archive/2019/">More from 2019</a>
GET /archive/2024/
<a href="/posts/2024/hello-world/">Hello World</a> <a href="/posts/2024/routing-deep-dive/">Routing Deep Dive</a> <a href="/posts/">All posts</a>
GET /blog/3/
HTTP/1.1 301 Moved Permanently Location: /posts/2019/django-tips/
GET /blog/999/ (no post with id 999)
HTTP/1.1 404 Not Found
Mounted under v2/ instead, the same names give /v2/posts/2019/django-tips/, and GET /v2/blog/3/ redirects there.
