1👍
So there are really a few different ways you could solve this particular problem based on your given code. I will outline a few of them so you ave some options moving forward.
The first option would be a simple helper method in the Post model. Something like this:
class Post(models.Model):
...
def get_unread_comment_count():
unread_comments = 0
for comment in comment_set:
unread_comments += 1
return unread_comments
Then you’d call that method in the template like so:
<p> unread messages: <span class="badge red">{{ post.get_unread_comment_count() }}</span></p>
Another option could be simply to put a field in the model itself.
class Post(models.Model):
...
unread_count = models.IntegerField(default=0)
This can get a bit costly as each time a new unread comment is added (which could be very frequent or not very often, not sure of the traffic this site will be getting) you have to access the database.
Final option would be maybe designing your own tag? I’m not familiar with this but you can get on Google and have a crack at it if you so desire.
Source:stackexchange.com