Code

Opened 3 years ago

Closed 3 years ago

Last modified 3 weeks ago

#15583 closed Uncategorized (wontfix)

Sorting dict by keys within django template

Reported by: foxwhisper 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

Attachments (0)

Change History (3)

comment:1 Changed 3 years ago by anonymous

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to wontfix
  • Status changed from new to closed

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

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

comment:2 Changed 7 months ago by anonymous

  • Easy pickings unset
  • Severity set to Normal
  • Type set to 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 Changed 3 weeks ago by anonymous

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.