在django模板中输出排序的python dict [英] Output sorted python dict within django template
问题描述
我想在django模板中显示这个dict,但是它应该被排序,所以它应该以A开头,然后继续使用B而不是H
这是我的dict(缩写):
{'A':[{'birthday_date':None ,
'first_name':'Alberto',
'last_name':'xxx',
'name':'Alberto xxx',
'uid':xxx},
{'birthday_date':无,
'first_name':'Antony',
'last_name':'xxx',
'name':'Antony xxx',
'uid':xxx}],
'H':[{'birthday_date':'08 / 28',
'first_name':'Hitoshi',
'last_name' xxx',
'name':'Hitoshi xxx',
'uid':xxx}],
'C':[{'birthday_date':'05 / 07/1985'
first_name':'chr',
'last_name':'xxx',
'name':'Chr xxx',
'uid':xxx}],
'E ':[{'birthday_date':None,
'first_name':'Edimond',
'last_name':'xxx',
'name':'Edimond xxx',
'uid':xxx},
{'birthday_date':'08 / 30',
'first_name':'Erika',
'last_name':'xxx',
'name':'Erika xxx',
'uid':xxx}],
'B':[{'birthday_date':'08 / 16/1987',
'first_name ':'Bhaktin',
'last_name':'xxx',
'name':'Bhaktin xxx',
'uid':xxx}],
' :[{'birthday_date':'08 / 25/1987',
'first_name':'Ivette',
'last_name :'xxx',
'name':'Ivette xxx',
'uid':xxx}]}
这是我的django模板:
{%for letter,friend in bdays_all.items% }
< h2> {{letter}}< / h2>
{%for friend in friend%}
...
{%endfor%}
{%endfor%}
这个功能很好,但没有订购。但这是我需要的。我尝试过python的sorted()函数,但没有任何成功。我只想订购信件。似乎是微不足道的,但我猜不是这样的。
任何想法?
p>
字典未排序。
您需要将您的dict转换为视图中的嵌套列表:最简单的方法是调用 sorted(bdays_all.items())
。
I got a python dict, that looks like the following (Important informations were replaced with "xxx" due to privacy reasons). I would like to display this dict within a django template but it should be ordered, so it should start with "A", then continue with "B" instead of "H"
This is my dict (shortened):
{ 'A': [ {'birthday_date': None,
'first_name': 'Alberto',
'last_name': 'xxx',
'name': 'Alberto xxx',
'uid': xxx},
{ 'birthday_date': None,
'first_name': 'Antony',
'last_name': 'xxx',
'name': 'Antony xxx',
'uid': xxx}],
'H': [ { 'birthday_date': '08/28',
'first_name': 'Hitoshi',
'last_name': 'xxx',
'name': 'Hitoshi xxx',
'uid': xxx}],
'C': [ { 'birthday_date': '05/07/1985',
'first_name': 'Chr',
'last_name': 'xxx',
'name': 'Chr xxx',
'uid': xxx}],
'E': [ { 'birthday_date': None,
'first_name': 'Edimond',
'last_name': 'xxx',
'name': 'Edimond xxx',
'uid': xxx},
{ 'birthday_date': '08/30',
'first_name': 'Erika',
'last_name': 'xxx',
'name': 'Erika xxx',
'uid': xxx}],
'B': [ { 'birthday_date': '08/16/1987',
'first_name': 'Bhaktin',
'last_name': 'xxx',
'name': 'Bhaktin xxx',
'uid': xxx}],
'I': [ { 'birthday_date': '08/25/1987',
'first_name': 'Ivette',
'last_name': 'xxx',
'name': 'Ivette xxx',
'uid': xxx}]}
This is my django template:
{% for letter, friend in bdays_all.items %}
<h2>{{ letter }}</h2>
{% for user in friend %}
...
{% endfor %}
{% endfor %}
This works well, but its not ordered. But thats what I need. I tried everything with python's sorted() function but without any successs. I only want to order the letters. Seems trivial but I guess it isn't.
Any ideas?
Thanks alot in advance!
Dictionaries are unsorted.
You will need to convert your dict to a nested list in the view: the easiest way would be just to call sorted(bdays_all.items())
.
这篇关于在django模板中输出排序的python dict的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!