Reorder comments from django comments framework in reverse order
-
22-06-2021 - |
문제
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)
제휴하지 않습니다 StackOverflow