简体   繁体   中英

Rendering ordered reversed dictionary value in Django template

Demo.html

I need output with sorted value of Coverage attribute.

<table>
<tr >
            <th>Test Case</th>
            <th>File Name</th>
            <th>Coverage</th>
        </tr>
           {% for key, value in d.items %}
           <tr>
               <td>{{ key }}</td>
           </tr>

                   {% for k,v in value.items|dictsortreversed:"0.lower" %}
              <tr>
                        <td>   </td>
                       <td>{{ k }}</td>
                       <td>{{ v }}</td>
            </tr>
             {% endfor %}
           {% endfor %}

</table>

I need to sort dictionary based on Coverage attribute i'm trying to do by using dictsort:"0.lower" but its sorting based on file name attribute but if i use dictsort:"1.lower" value is not printing.I need sorting on value (Coverage).

Please do help me out.

In your views.py, import operator; in your context dictionary instead of putting the original dictionary, put the srtd.

import operator
original_dictionary = {}
srtd = sorted(original_dictionary.items(), key=operator.itemgetter(1))

And finally in your html you can simply do

{% for key, value in srtd %}
{{k}}{{v}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM