2

I would like to reorder my comments to display newest first. I am using the built in Django comments framework. Is there a built in, or easy way to do this?

cjohnston
  • 135
  • 6

2 Answers2

6

From the Django docs:

You can loop over a list in reverse by using {% for obj in list reversed %}.

So, in my template I have:

{% for comment in comment_list reversed %}

Your comments are now in reverse.

tfitzgerald
  • 176
  • 1
  • 4
0

You can try https://docs.djangoproject.com/en/dev/ref/contrib/comments/custom/

class ReorderComment(Comment):
    class Meta:
        ordering = ["-submit_date"]

on the settings.py

COMMENTS_APP = 'my_comment_app' 

Or you can reoder them by creating a templatetags

{% get_comment_list for event as comment_list %}
{% reoder_comments comment_list as reodered_comment_list %}

The reoder templatetags will looks like (with django-classy-tags)

register = template.Library()
class ReoderComments(Tag):
    name = 'reoder_comments'
    options = Options(
        Argument('queryset'),
        'as',
        Argument('varname', required=False, resolve=False)
    )
    def render_tag(self, context, queryset, varname):
        context[varname] = queryset.order_by("-submit_date")
        return ''
register.tag(ReoderComments)
surfeurX
  • 1,262
  • 12
  • 16
  • Using the templatetag method, I am now getting an error saying "Exception Value: name 'Tag' is not defined". Could you please provide further guidance? – cjohnston Jul 23 '12 at 22:05