Dictsort filter filter and two-column filtering

In the template, now I use this:

{% for item in mydict|dictsortreversed:"column1" %}

But I need to sort the data by two columns - is there any option in dictsort? Or any alternative to do this in a template?

+5
source share
2 answers

As you can see in the Django code there is no support.

However, since Python is sorted in a stable way . This is trivial to implement by sorting twice :)

{% for item in mydict|dictsortreversed:"column1"|dictsortreversed:"column2" %}
+9
source

How about something like -

{% for item in mydict|dictsortreversed %}
    {% if forloopcount|diviisbleby:"2" %}
            <td> item </td> 
        </tr>
    {% else %}
        <tr>
            <td> item </td>
    {% endif %}
{% endfor %}

Look counter forloop and divisible by template tags for more ideas.

0
source

All Articles