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

Named Routes And Redirects

easy100%3 of 3 solved

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:

idtitleslugyear
1Hello Worldhello-world2024
2Routing Deep Diverouting-deep-dive2024
3Django Tipsdjango-tips2019

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):
PathViewNamespaced nameExample URL
posts/post_listblog:post-list/posts/
posts/<year>/<slug>/post_detailblog:post-detail (year, slug)/posts/2024/hello-world/
archive/<year>/post_archiveblog:post-archive (year)/archive/2019/
blog/<id>/legacy_post_redirectnot 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.

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 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.