Login

sortby template tag

Author:
showell
Posted:
July 1, 2009
Language:
Python
Version:
1.0
Tags:
template dictsort
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. "If in" template tag by andrew 7 years, 1 month ago
  2. InlineFormset Template by petry 6 years, 6 months ago
  3. partition template filters by SmileyChris 7 years, 8 months ago
  4. django image bundle exaple template by amitu 7 years, 11 months ago
  5. Template Tag Caveat by ericmoritz 6 years, 11 months ago

Comments

Please login first before commenting.