Modifying the views
Now we have to change the parameters of the post_detail
view to match the new URL parameters and use them to retrieve the corresponding Post
object.
Edit the views.py
file and edit the post_detail
view like this:
def post_detail(request, year, month, day, post):
post = get_object_or_404(Post,
status=Post.Status.PUBLISHED,
slug=post,
publish__year=year,
publish__month=month,
publish__day=day)
return render(request,
'blog/post/detail.html',
{'post': post})
We have modified the post_detail
view to take the year
, month
, day
, and post
arguments and retrieve a published post with the given slug and publication date. By adding unique_for_date='publish'
to the slug
field of the Post
model before, we ensured that there will be only one post with a slug for...