On 19 November 2010 19:53, Adam Hallett <harddriveca...@gmail.com> wrote:
> Just a thought, what about:
>
> {% for k,v in mydict|order:'key' %}
>
> or
>
> {% for k,v in mydict orderby key %}

Here's a quick hack that works right now:

{% for k, v in mydict.items|dictsort:"0" %}
  {{ k }}: {{ v }}
{% endfor %}

> On Nov 19, 10:55 am, Paweł Roman <romapa...@googlemail.com> wrote:
>> When rendering dictionary, there is absolutely no way to display
>> values sorted by keys. The only workaround is to copy all the dict
>> items to a sortable structure e.g. list and pass the list to the
>> template, instead of the dictionary.

Keep in mind that sorting a built-in dictionary in Python will always
require some copying, because it's a hashtable. If you need a sorted
dictionary, you must write a new datastructure like in this recipe[1]

>>
>> Why not introduce a new tag to solve this problem:
>>
>> I say, it should be called {%secretfor%} and the usage would be
>>
>> {%secretfor k,v in mydict %}
>> ... do stuff....
>> {%endsecretfor%}
>>
>> would work like sorted(mydict.keys)
>>
>> I won't stick with the tag name, but you see my point.

The name is *really* awkward - what does sorting have to do with "secret"?

-- 
Łukasz Rekucki

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to