1👍
✅
You can use django template tags to do it..
Under myapp/templatetags/
create a myapp_tags.py
and __init__.py
In myapp_tags.py
do the following:
from django import template
register = template.Library()
@register.assignment_tag
def get_top_list():
top_list = User.objects.filter().order_by('-score')[:10]
return top_list
And then use it in your templates like this:
{% load myapp_tags %}
{% get_top_list as top_list %}
{% for top_user in top_list %}
...
Source:stackexchange.com