views:

356

answers:

2

What's the Django way of presenting a formset horizontally, i.e. one row per form? The as_table method generates multiple forms vertically (with the labels). I need the form fields in table rows (one row per form) and the labels should be on top. I don't see anything out of the box. Is this discouraged for some reason?

I should clarify that I actually want a table, because of a UI table widget I'll be using. And that table should have the labels in the .

So my desired structure is:

<table>
  <thead>
     <tr><th>column1</th><th>column2</th></tr>
  </thead>
  <tbody>
    <tr><td>form1.value1</td><td>form1.value2</td></tr>
...
  </tbody>
</table>
+1  A: 

I suggest using form.as_ul and styling it with your CSS to make it all on one row. You can do that with ul li { display: inline; } or of course, substitute a class or ID if you don't want to affect all ULs in that manner.

Here's the relevant section of the Django docs: http://docs.djangoproject.com/en/dev/topics/forms/#displaying-a-form-using-a-template

Edit: To address your need for a table, you'd like want to do something like this... edited some more.

It's difficult to put all of these forms in a table, and still have valid HTML. A form element can surround a table, or be inside a <td>... though this will likely still work.

<thead>
  <tr>
   {% for field in form %}
     <th>{{ field.label }}</th>
   {% endfor %}
  </tr>
</thead>

<tbody>
 <tr class="table_row">
  <form action="/something/" method="post">
    {% for field in form %}
      <td>
       <table>
        <tr><td>{{ field.label }}</td></tr>
        <tr><td>{{ field }}</td></tr>
       </table>
      </td>
    {% endfor %}
   </form>
  </tr>
 </tbody>
Alex JL
See my clarification please. I actually *need* a table.
kmt
I think you're going to have to go with the customization in your template, then, instead of using the methods that automatically generate HTML. Then you can put it all in one `<tr>`.
Alex JL
I see your update. Thanks! But, well, I need the labels to be in the <thead>, above all rows, not repeating for each individual row.I basically need a table representation of a recordset. Find it surprising that I couldn't find this googling.
kmt
Okay, you'd just want to go over the elements the first time and print only the labels, as `th` s in the `thead`, then print the rest of your forms.
Alex JL
Yes. But then take care of the errors too, etc. It becomes the snippet that Dave here posted. I appreciate your answers though. Being new at Django, I appreciate any ideas.
kmt
+4  A: 

you might want to try something like this http://www.djangosnippets.org/snippets/1442/

Dave
Yes, this does it. It is a verbose thing to put in your templates. With something so verbose, you'd want it either as a as_table-like method or you'd want to have the ability to call templates with a parameter (not just include them). I am really surprised that something as mature as Django doesn't have this out of the box.
kmt
I typically add this as a generic template (saved as formset_table.html) then for each formset template i pass {% include "formset_table.html" %}, but i agree it would be nice of it could be rendered as a builtin method.
Dave
I ended up doing the same. And because I have a page with multiple such forms I ended up doing using the with tag to parameterize it.
kmt