Is there a good way to render the enumeration of a queryset into two div
columns?
Using 960 grid, I've got something to the effect of...
<div class="container_16">
<div class="grid_8 alpha"></div>
<div class="grid_8 omega"></div>
</div>
In Django, one model needs to have it's enumerated contents rendered in both of those columns, and preferably somewhat equally. For the moment, I've got some ugly code that in the view splits the QuerySet into 2 halves, and then each half is rendered in their respective column.
There's got to be a better way to do this, preferably using only the template rendering system?
Just for reference, here's how it "works" at the moment:
views.py
@render_to('template.html')
def main_athletics_page(request, *args, **kwargs):
sports = Sport.objects.all()
half = sports.count() / 2
return { 'sports_1' : sports[0:half], 'sports_2' : sports[half:] }
template.html
<div class="grid_8 alpha">
{% for sport in sports_1 %}
<!-- Blah blah -->
{% endfor %}
</div>
<div class="grid_8 omega">
{% for sport in sports_2 %}
<!-- Blah blah -->
{% endfor %}
</div>
I think you will have to make your own template tag to do splits on queries. I would do something like.
Please note that I haven't actually tested this code, so it might fail spectacularly, but it should point you towards the right direction for getting that stuff out of your view.
Here's a quick solution which uses bootstrap and needs no Django filters
Use default Django template tag
cycle
:It will display your list [1 2 3 4 5 6] as
1 2
3 4
5 6
By the way, Jinja2 template engine has
batch
andslice
filters which will do the trick. I switched to jinja2 and now have none of those problems of "how to display x using poor django tags and filters"I recommend using Django filters.
Django snippets provides a partitioning template filter, which you can use like: