from django.template import Library import datetime register = Library() @register.filter def past_days(num_days): """ Returns a list of date objects for a given number of past days, including today. Useful for summaries of recent history. Usage: {% load past_days %} """ days = [] day = datetime.date.today() - datetime.timedelta(num_days - 1) for _ in xrange(num_days): days.append(day) day += datetime.timedelta(1) return days