2011-11-04 4 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': }} 

순서를 참고 : - 87 대신에 88을 사용합니다.

나는 87 개의 다음부터 시작하고 싶습니다. d에 의해 88 및 89.

답변

1

대신 OrderedDict을 사용해보십시오. 정규 파이썬 사전은 순서가 없습니다 - 키 순서를 뒤 틀릴 해시 테이블을 사용하여 구현됩니다.

docs for dictionaries : "사전을 순서가없는 키 : 값 쌍으로 생각하는 것이 가장 좋습니다. 그 이유는 키가 고유하다는 것 (하나의 사전 내에서)이라는 것입니다."

+0

위대한 OrderDict가 유용 할 것 같습니다. –

4

사전은 순서가 지정되지 않습니다. 대신 중첩 목록을 사용하십시오.

관련 문제