Login

sortby template tag

Author:
showell
Posted:
July 1, 2009
Language:
Python
Version:
1.0
Score:
1 (after 1 ratings)

This is a variation on dictsort that assumes objects with attributes, not dictionaries.

Example usage:

{% for book in author.book_set.all|sortby:'title' %}

1
2
3
4
5
6
7
8
9
from django.template import Library
register = Library()

@register.filter
def sortby (sequence, attribute):
    # variation on dictsort using attribute access
    lst = list(sequence)
    lst.sort(key=lambda obj: getattr(obj,attribute))
    return lst

More like this

  1. A wrapper around cache_page making it optional by rixx 6 days, 10 hours ago
  2. Treat empty value on DateRangeFilter as a filter for non filled dates by kmmbvnr 1 month, 2 weeks ago
  3. Django Settings Assignment Expressions aka Walrus Operator example by webology 2 months, 2 weeks ago
  4. codigo alto nivel by MrRocklion 4 months, 1 week ago
  5. Load template from specific app by Krzysiek555 5 months ago

Comments

Please login first before commenting.