Login

grouper tag

Author:
visgean
Posted:
August 8, 2011
Language:
Python
Version:
1.3
Tags:
template tag templatetag formset
Score:
0 (after 0 ratings)

I needed to display formset into table and I didnĀ“t like solution I have found. So I have written this simple tag you can use it in templates like this:

{% for row in formset|square_it:6 %} <tr> <td> </td> {% for form in row %} <td> {% for field in form %} {{ field }} {% endfor %} </td> {% endfor %}

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
@register.filter
def square_it(l, n):
    """
    This filter squares|groups list into smaller lists of given number of elements
    >>>print square_it(range(74), 4)
    [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23], [24, 25, 26, 27], [28, 29, 30, 31], [32, 33, 34, 35], [36, 37, 38, 39], [40, 41, 42, 43], [44, 45, 46, 47], [48, 49, 50, 51], [52, 53, 54, 55], [56, 57, 58, 59], [60, 61, 62, 63], [64, 65, 66, 67], [68, 69, 70, 71], [72, 73]]
    
    """
      
    return [l[i:i+n] for i in range(0, len(l), n)]

More like this

  1. Group sequence into rows and columns for a TABLE by davidwtbuxton 4 years ago
  2. NewForms Readonly / Edit Pattern by FreddieP 7 years, 2 months ago
  3. Sort Table Headers by insin 7 years, 7 months ago
  4. Form row filter by kylefox 7 years ago
  5. Functional Filters by waterson 7 years, 5 months ago

Comments

Please login first before commenting.