Login

List divide columns across

Author:
Leon
Posted:
May 17, 2009
Language:
Python
Version:
1.0
Score:
1 (after 1 ratings)

I wanted a way to make columns but have them in floated uls...

 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
from django.template import Library

register = Library()

@register.filter
def columnsacross(list, n):
	"""
	Divide list into (n) of columns going across
	
	Example:
	{% for column in object_list %}
	<ul>
		{% for item in column %}
			<li>{{ item }}</li>
		{% endfor %}
	</ul>
	{% endfor %}
		
	>>> list = range(1, 11)
	>>> columnsacross(list, 4)
	[[1, 5, 9], [2, 6, 10], [3, 7], [4, 8]]
	>>> columnsacross(list, 3)
	[[1, 4, 7, 10], [2, 5, 8], [3, 6, 9]]
	"""
	try:
		n = int(n)
	list = list(list)
		except (ValueError, TypeError):
		return [list]
        
	return [list[i::n] for i in range(n)]
	
if __name__ == "__main__":
    import doctest
    doctest.testmod()

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 3 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.