Adding comments to the post detail template

We have created the functionality to manage comments for a post. Now, we will need to adapt our post/detail.html template to do the following things:

  • Display the total number of comments for the post
  • Display the list of comments
  • Display a form for users to add a new comment

First, we will add the total comments. Open the post/detail.html template and append the following code to the content block:

{% with comments.count as total_comments %}  <h2>    {{ total_comments }} comment{{ total_comments|pluralize }}  </h2>{% endwith %}

We are using the Django ORM in the template, executing the QuerySet comments.count(). Note that Django template language doesn't use parentheses for calling methods. The {% with ...

Get Django 2 by Example 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.