Answer the question
In order to leave comments, you need to log in
How does the as argument work?
Please explain how it works
{% get_most_commented_posts as most_commented_posts %}
<ul>
{% for post in most_commented_posts %}
<li>
<a href="{{ post.get_absolute_url }}">{{ post.title }}</a>
</li>
{% endfor %}
</ul>
def get_most_commented_posts(count=5):
return Post.published.annotate(total_comments=Count('comments')).order_by('total_comments')[:count]
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question