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 ...