put columns in rows

13 views
Skip to first unread message

Alessandro Ronchi

unread,
May 28, 2008, 10:16:06 AM5/28/08
to django...@googlegroups.com
I want to show my query results in rows instead of columns in an html page, but
I don't know if it's possible in templates, because of
<tr><td>1</td><td>2</td></tr>
<tr><td>3</td><td>4</td></tr>
structure

how can I do?
--

Alessandro Ronchi
Skype: aronchi
http://www.alessandroronchi.net - Il mio blog
http://www.soasi.com - Sviluppo Software e Sistemi Open Source
http://www.animalisenzacasa.org - Il portale delle adozioni gratuite di animali

Tim Chase

unread,
May 28, 2008, 12:22:58 PM5/28/08
to django...@googlegroups.com
> I want to show my query results in rows instead of columns in
> an html page, but I don't know if it's possible in templates,
> because of
> <tr><td>1</td><td>2</td></tr>
> <tr><td>3</td><td>4</td></tr>
> structure
>
> how can I do?

I've solved this in the past by creating this filter:

==========================================
from itertools import islice

def columnize(data, args="2"):
if ',' in args:
columns, padding = args.split(',', 1)
columns = int(columns)
else:
columns = int(args)
padding = ""
i = iter(data)
while True:
data = list(islice(i, columns))
ld = len(data)
if not ld: break
if ld <> columns:
data.extend([padding] * (columns - ld))
yield data
==========================================


and then, after registering it as a filter[1], using it like this:

-------------------------------------------------
<table>
{% for myrow in data|columnize:"3,&nbsp;" %}
<tr>
{% for value in myrow %}
<td>{{ value }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>

or

<table>
{% for thing in my_queryset|columnize %}
<tr>
{% for value in myrow %}
<td>
{% if value %}
<b>{{ value.name }}:</b>
{{ value.other_field }}
{% else %}
&nbsp;
{% endif %}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
-------------------------------------------------

My original can be found here[2] but I think this version is a
little easier to understand than the original.

Hope this helps,

-tim

[1]
http://www.djangoproject.com/documentation/templates_python/#writing-custom-template-filters

[2]
http://groups.google.com/group/django-users/browse_thread/thread/a9ca56a9f601271a/

Alessandro Ronchi

unread,
May 29, 2008, 7:51:01 AM5/29/08
to django...@googlegroups.com
Tim Chase ha scritto:


I have two questions.

1) I cannot find where must I write the register of the filter
2) I have a structure like this:
(caption1, caption2, caption3, caption4)
(a,b,c,d)

I want to print it like:
<table>
<tr><td>Caption1</td><td>A</td></tr>
<tr><td>Caption2</td><td>B</td></tr>
<tr><td>Caption3</td><td>C</td></tr>
<tr><td>Caption4</td><td>D</td></tr>

How can I do that? It seems a bit different than your, because you don't have
captions...
--

Alessandro Ronchi
Skype: aronchi

SOASI Soc.Coop. - www.soasi.com


Sviluppo Software e Sistemi Open Source

Sede: Via Poggiali 2/bis, 47100 Forlì (FC)
Tel.: +39 0543 798985 - Fax: +39 0543 579928

Rispetta l'ambiente: se non ti è necessario, non stampare questa mail

Tim Chase

unread,
May 29, 2008, 12:52:58 PM5/29/08
to django...@googlegroups.com
> I have two questions.
>
> 1) I cannot find where must I write the register of the filter

Details on creating your own filters are at the
writing-custom-template-filters link I gave above. There's some
additional information above that particular link as well if
you've never created a template tag/filter before. It's
remarkably easy...just don't forget to load the tag/filter
library in your template.

> 2) I have a structure like this:
> (caption1, caption2, caption3, caption4)
> (a,b,c,d)
>
> I want to print it like:
> <table>
> <tr><td>Caption1</td><td>A</td></tr>
> <tr><td>Caption2</td><td>B</td></tr>
> <tr><td>Caption3</td><td>C</td></tr>
> <tr><td>Caption4</td><td>D</td></tr>
>
> How can I do that? It seems a bit different than your, because you don't have
> captions...

well, I did what was requested...I'll have to check my DWIM
functionality. ;-)

If you want to just combine them, you can use python's zip() command:

captions = ["Cap1", "Cap2", "Cap3"]
data = ['A', 'B', 'C', 'D']
pairs = zip(captions, data)
# pass "pairs" to your template

then in your template you can use

{% for caption, datum in pairs %}
<tr>
<td>{{ caption }}</td>
<td>{{ datum }}</td>
</tr>
{% endfor %}

or, if you're not using trunk you may have to use a single
variable like

{% for pair in pairs %}
<tr>
<td>{{ pair.0 }}</td>
<td>{{ pair.1 }}</td>
</tr>
{% endfor %}

While it would be possible to make a "zip" tag that would do this
for you, IMHO, it really belongs in the view as it asserts an
association between two data-sets (and thus is not presentational
which would be a criterion for template tags in my book).

-tim

Reply all
Reply to author
Forward
0 new messages