2011-11-04 52 views
0

我有一本以下字典。变量名是division_total_displayDjango模板字典显示顺序错误

 
{87L: {'name': , 
     'total': 660L, 
     'total_percentage': 39, 
     'total_right': 256L}, 
88L: {'name': , 
     'total': 660L, 
     'total_percentage': 42, 
     'total_right': 274L}, 
89L: {'name': , 
     'total': 435L, 
     'total_percentage': 34, 
     'total_right': 148L}} 

我传递给模板如下:

return render_to_response('site/report_topic_standard_stat.html', 
     { 
      'report_date' : report_date, 
      'et_display' : et_display, 
      'stats_display' : stats_display, 
      'division_total_display' : division_total_display, 
      'school' : school, 
      'board' : board, 
      'standard' : standard, 
      'standard' : standard, 
      'subject' : subject, 
      'from_time' : from_time, 
      'term_id' : term_id, 
      'white_label' : white_label, 
     }, 
     context_instance=RequestContext(request) 

然而,在当我打印模板{{division_total_display}}

 
{88L: {'total_right': 274L, 'total': 660L, 'total_percentage': 42, 'name': }, 
89L: {'total_right': 148L, 'total': 435L, 'total_percentage': 34, 'name': }, 
87L: {'total_right': 256L, 'total': 660L, 'total_percentage': 39, 'name': }} 

请注意顺序: - 其开始与88而不是87.

我希望它开始87跟随d除以88和89.

回答

1

尝试使用OrderedDict代替。普通的python字典是无序的 - 它们是使用散列表来实现的,这些散列表将加密密钥顺序。

根据docs for dictionaries:“最好将字典视为一组无用的键:值对,并且要求键是唯一的(在一个字典中)。”

+0

Great OrderDict似乎很有用。 –

4

字典无序。改为使用嵌套列表。