Opened 14 years ago

Closed 14 years ago

Last modified 4 weeks ago

#15583 closed Uncategorized (wontfix)

Sorting dict by keys within django template

Reported by: Cal Leeming Owned by: nobody
Component: Template system Version: 1.2
Severity: Normal Keywords: sort, dict, keys, template, sorting, sorted, dictionary
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Currently, there is no way to sort a dict by its keys within a django template without doing mods within the view. This really should just work out of the box.

Test case:

{'a': 'value 1',
 'c': 'value 3',
 'b': 'value 2',
}
{% for key, val in dict.items|sort %}
key: {{key}} / {{value}}
{% endfor %}

Current functionality:

key: a / value 1
key: c / value 3
key: b / value 2

Functionality with sort patch:

key: a / value 1
key: b / value 2
key: c / value 3

Someone has already written the appropriate code to do this (as a custom filter), and seems to work fine. I'd like to see this taken up for consideration to be put merged into the core, so you can use this filter without needing to put it in via custom template tags.

Sadly, I haven't got time to write the necessary core changes, test it, and submit as a patch file. If I get a chance before anyone else does, I'll get this done.

#File: custom_filters.py
# Author: http://cookingupawebsiteindjango.blogspot.com/2009/05/custom-template-filters-manipulating.html
from django import template
from django.utils.datastructures import SortedDict

register = template.Library()

@register.filter(name='sort')
def listsort(value):
  if isinstance(value,dict):
    new_dict = SortedDict()
    key_list = value.keys()
    key_list.sort()
    for key in key_list:
      new_dict[key] = value[key]
    return new_dict
  elif isinstance(value, list):
    new_list = list(value)
    new_list.sort()
    return new_list
  else:
    return value
listsort.is_safe = True

Change History (4)

comment:1 by anonymous, 14 years ago

Resolution: wontfix
Status: newclosed

You can sort the keys in a list beforehand or use SortedDict

http://code.djangoproject.com/wiki/SortedDict

comment:2 by anonymous, 11 years ago

Easy pickings: unset
Severity: Normal
Type: Uncategorized
UI/UX: unset

I'd like to point out that SortedDict doesn't solve the problem as it requires the keys to be inserted in their intended order to begin with.

If I'm making a deep dictionary to print a complicated table, but am inserting things in an arbitrary order (or received it that way), I now need to also walk my dictionary and resort everything. And then walk it again to render it.

It would be SOOO much simpler if I were able sort it arbitrarily at render time in the template.

comment:3 by anonymous, 11 years ago

I have a similar problem, I have a complex dictionary and would be incredibly easy to sort at render time by key or value(s), The SortedDict does not solve my problem. It requires me to change my data to a list of tuples, which would cause a work around but is causing me headaches as I am using javascript libraries to visualize this large complex data. For some reason the built - in (Python) Sorted is not working from within a view - which i don't quite understand but these problems seem to stem from Django not python

comment:4 by Collin Anderson, 4 weeks ago

For people like me finding this page via search engine: Dict sorting by key can accomplished by {{ dict.items|dictsort:0 }} ever since #25670.

https://docs.djangoproject.com/en/stable/ref/templates/builtins/#std-templatefilter-dictsort

dictsort can also order a list of lists (or any other object implementing __getitem__()): {{ value|dictsort:0 }}

Note: See TracTickets for help on using tickets.
Back to Top