user606521
user606521

Reputation: 15474

dictsort template filter and filtering with two columns

In template for now I am using this:

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

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

Upvotes: 7

Views: 3488

Answers (2)

Wolph
Wolph

Reputation: 80111

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

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

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

Upvotes: 15

Aidan Ewen
Aidan Ewen

Reputation: 13328

How about something like -

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

Check out the forloop counter and divisible by template tags for more ideas.

Upvotes: 0

Related Questions