1

I having a dictionary

a = {'name': u'45445454',
     'tracks': [{'A_TITLE': u'abb',
             'IS': u'144',
             'PN': u'3',
             'T_TITLE': u'123'},
            {'A_TITLE': u'abb',
             'IS': u'45454454',
             'PN': u'3',
             'T_TITLE': u'225'},
            {'A_TITLE': u'ggg',
             'IS': u'232',
             'PN': u'000',
             'T_TITLE': u'555'}]}

<table>

{% for e in tracks %}
<tr> <td> Title </td> <td> {{ e.A_TITLE }}  -  PN {{ e.PN }}</td></tr>
<tr> <td> Name </td><td> {{e.T_TITLE }} - IS {{e.IS }} </td></tr>
   {% endfor %}

</table>

Now it was printing like

 <tr> <td>TITLE </td><td> abb - PN 3 </td></tr>
 <tr> <td>NAME </td><td> 123 - IS 144 </td></tr>
 <tr> <td>TITLE </td><td> abb - PN 3 </td></tr>
 <tr> <td>NAME </td><td> 225 - IS 45454454 </td></tr>
 <tr> <td>TITLE </td><td> ggg - PN 000 </td></tr>
 <tr> <td>NAME </td><td> 555 - IS 232 </td></tr>

Note: A_TITLE is depends on PN
I want it arrange by A_TITLE.

Like

 <tr> <td>TITLE </td><td> abb - PN 3 </td></tr>
 <tr> <td>NAME </td><td> 123 - IS 144 </td></tr>
 <tr> <td>NAME </td><td> 225 - IS 45454454 </td></tr>
 <tr> <td>TITLE </td><td> ggg - PN 000 </td></tr>
 <tr> <td>NAME </td><td> 555 - IS 232 </td></tr>

I am struggling in this a more time ,

Thanks in Advance.

4
  • I think you want to group the for/loop with A_TITLE? use regroup Commented Apr 17, 2015 at 11:20
  • 1
    look at Django templates builtin regroup Commented Apr 17, 2015 at 11:20
  • @Anzel Could you please post your answer related with my senario Commented Apr 17, 2015 at 11:22
  • Just look at @catavaran's even better solution Commented Apr 17, 2015 at 11:30

1 Answer 1

1

You can use the combination of the dictsort filter and {% ifchanged %} template tag:

{% for e in tracks|dictsort:"A_TITLE" %}

    {% ifchanged %}
        <tr><td>Title</td><td>{{ e.A_TITLE }}  -  PN {{ e.PN }}</td></tr>
    {% endifchanged %}

    <tr><td>Name</td><td>{{e.T_TITLE }} - IS {{e.IS }}</td></tr>

{% endfor %}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.