URL Routing and Article Detail Page
In this chapter you will learn Django's dynamic route configuration, implementing article detail pages and links between pages.
What is a dynamic route?
The homepage shows all articles, and each article needs its own independent detail page.
The URL should look like this:/post/1/、/post/2/, where the numeric part is the article's primary key (pk).
Dynamic routes allow variables in the URL, and Django automatically extracts and passes them to the view function.
Configure dynamic routes
Example
File path: blog/urls.py
from
django.
urls
import
path
from
.
import
views
urlpatterns
=
[
path
(
''
,
views.
index
,
name
=
'index'
)
,
int:pk — an integer parameter in the URL named pk (primary key)
name='post_detail' — name the route, referenced in templates with {% url %}
path
(
'post/int:pk/'
,
views.
post_detail
,
name
=
'post_detail'
)
,
]
Route converter descriptions:
| Converter | Example path | Matching Rules |
|---|---|---|
|
/post/3/ | Match a positive integer |
|
/post/django-guide/ | Match a string (excluding /) |
|
/post/django-guide/ | Match letters, digits, -, _ |
|
/post/uuid/ | Match UUID format |
Write the detail page view
The detail page needs to query the corresponding article from the database based on the pk in the URL.
Example
File path: blog/views.py
from
django.
shortcuts
import
render
,
get_object_or_404
from
.
models
import
Post
def
post_detail
(
request
,
pk
)
:
"""Article detail page"""
get_object_or_404: query a single record
If found, return the object; if not found, automatically return a 404 page (no try/except needed)
post
=
get_object_or_404
(
Post
,
pk
=
pk
)
context
=
{
'post'
: post
,
'title'
: f
'{post.title} - RUNOOB Blog'
}
return
render
(
request
,
'blog/post_detail.html'
,
context
)
get_object_or_404is Django's best practice. It is equivalent toPost.objects.get(pk=pk)+try/except Post.DoesNotExist, but the code is more concise. When users access a non-existent article ID, they see a friendly 404 page instead of an error.
Create the detail page template
Example
{% extends 'blog/base.html' %}
{% block title %}{{ post.title }} - RUNOOB Blog{% endblock %}
{% block content %}
{{ post.category.name }}
{{ post.title }}
{{ post.content|safe }}
← Back to Home
{% endblock %}
Detail page styles
Example
/* append to the style in base.html */
.post-view
{
max-width
:
720px
;
margin
:
0
auto
;
}
.category-tag
{
display
:
inline-block
;
padding
:
4px
12px
;
background
:
#e8f5e9
;
color
:
#2e7d32
;
border-radius
:
12px
;
font-size
:
13px
;
margin-bottom
:
12px
;
}
.post-view
h1
{
font-size
:
32px
;
margin-bottom
:
12px
;
line-height
:
1.4
;
}
.post-view
time
{
display
:
block
;
color
:
#999
;
font-size
:
14px
;
margin-bottom
:
30px
;
}
.content
{
line-height
:
1.8
;
font-size
:
16px
;
color
:
#333
;
}
.content
h2
{
margin
:
24px
0
12px
;
font-size
:
22px
;
}
.content
p
{
margin-bottom
:
12px
;
}
.back-link
{
display
:
inline-block
;
margin-top
:
40px
;
color
:
#2c3e50
;
text-decoration
:
none
;
}
|safeThe safe filter tells Django: this content is safe HTML, render it directly without escaping. When using safe, make sure the content is trustworthy (such as content you edited yourself in Admin). If it is user-submitted content, never add safe, otherwise there is an XSS risk.
Generating links in templates: {% url %}
Add a detail page link to the article cards on the homepage.
Example
{% for post in posts %}
{{ post.category.name }}
{{ post.title }}
{{ post.summary|truncatechars:80 }}
{{ post.created_at|date:"Y-m-d" }}
{% endfor %}
{% url 'post_detail' post.pk %} automatically generates the correct URL based on the route name and parameters.
If the route changes later (such as /post/ changed to /article/), the links in the template are automatically updated without manual modification.
Add get_absolute_url() to the model
Defining this method in the Post model lets the Admin backend, Django's built-in tools, etc. automatically find the detail page link.
Example
File path: add to the Post class in blog/models.py
from
django.
urls
import
reverse
class
Post
(
models.
Model
)
:
... field definitions ...
def
str
(
self
)
:
return
self
.
title
def
get_absolute_url
(
self
)
:
reverse looks up a URL based on the route name and parameters
return
reverse
(
'post_detail'
,
kwargs
=
{
'pk'
:
self
.
pk
}
)
Chapter Summary
In this chapter you mastered the core of Django routing: dynamic routes pass parameters, get_object_or_404 for safe queries, the {% url %} template tag to generate links, and reverse to look up URLs.
Now the blog has two pages: the homepage list and the article detail page; clicking a card takes you to read the full article.