Creating Django's detail views

A detail view is a way to specify what should be included and displayed on a web page. This corresponds to one specific model object. Here, we will add in the logic for listing and viewing the content of the individual blog posts that have been saved in our database. First, we need to extend the logic of the Post class:

  1. In the blog/models.py file, add the following method to our current Post class:
from django.urls import reverseclass Post(models.Model):    ...    def get_absolute_url(self):        return reverse('blog:post_detail', args=[            self.publish_date.year,            self.publish_date.strftime('%m'),            self.publish_date.strftime('%d'),            self.slug        ])

Here, we are specifying that when a Post object calls its get_absolute_url() ...

Get Hands-On Application Development with PyCharm now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.