Jignesh Vasani
Jignesh Vasani

Reputation: 1

Django template dictionary displays in a wrong order

I have a following dictionary. Variable name is division_total_display

{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}}

I pass to template as follows:

    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)

However in the template when I print {{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': }}

Please note the ordering:- Its starts of with 88 instead of 87.

I want it to start with 87 followed by 88 and 89.

Upvotes: 0

Views: 714

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599490

Dictionaries are unordered. Use a nested list instead.

Upvotes: 4

Raymond Hettinger
Raymond Hettinger

Reputation: 226231

Try using an OrderedDict instead. Regular python dictionaries are unordered -- they are implemented using hash tables which will scramble the key order.

Per the docs for dictionaries: "it is best to think of a dictionary as an unordered set of key: value pairs, with the requirement that the keys are unique (within one dictionary)."

Upvotes: 1

Related Questions