Modifying the canonical URL for posts
We also have to modify the parameters of the canonical URL for blog posts to match the new URL parameters.
Edit the models.py
file of the blog
application and edit the get_absolute_url()
method as follows:
class Post(models.Model):
# ...
def get_absolute_url(self):
return reverse('blog:post_detail',
args=[self.publish.year,
self.publish.month,
self.publish.day,
self.slug])
Start the development server by typing the following command in the shell prompt:
python manage.py runserver
Next, you can return to your browser and click on one of the post titles to take a look at the detail view of the post. You should see something like this:
Figure 2.1: The page for the post’s detail view
Take a look at the URL—it should look like /blog/2022/1/1/who-was-django-reinhardt...