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
- Template tag - list punctuation for a list of items by shapiromatron 11 months ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 1 week ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Please login first before commenting.