Turikumwe's answer forced me to close, but did not work for my environment: python3 and Django 1.10.
I found that the filter is called using
{% for key, value in companies.items|sort %} {{ key }} {{ value }} {% endfor %}
actually leads to an ItemsView, not a dict. (I suspect this is a problem with python 2 vs 3). Given ItemView, the answer is even simpler
from django import template from django.utils.datastructures import ItemsView register = template.Library() @register.filter(name='sort') def listsort(value): if isinstance(value, ItemsView) or isinstance(value, list): return sorted(value) else: return value
jim anderson
source share