Tim Chase ha scritto: >> 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, " %} > <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 %} > > {% 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/
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---