> 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/










--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to