문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top