Django: Get the latest comments for a particular object

Its easy enough if you're using the template tags. Just refer to the documentation and you should be set.

First, remember to load the comments tags.

{% load comments %}

To get the number of comments for an object:

{% get_comment_count for entry as comment_count %}

Or quickly display all the comments:

{% render_comment_list for whatever_object %}

If you want more control displaying the comments:

{% get_comment_list for object as comment_list %}

{% for comment in comment_list %}
{% endfor %}

However if you're trying to get that info in the code, good luck.

from django.contrib.comments.models import Comment
from django.contrib.sites.models import Site
site = Site.objects.get_current()
comments = Comment.objects.for_model(self).filter(site = site, is_public = True, is_removed = False)


[ Source ]

Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog