Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontal (per-row) forms in a Django formset

Tags:

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> 
like image 411
kmt Avatar asked Feb 10 '10 02:02

kmt


People also ask

What is inline formset in Django?

Django formset allows you to edit a collection of the same forms on the same page. It basically allows you to bulk edit a collection of objects at the same time.

How many types of Django forms are there?

Django form fields define two types of functionality, a form field's HTML markup and its server-side validation facilities.

Which function of Django's form class will render a form's fields as a series of P tags?

{{ form. as_p }} – Render Django Forms as paragraph.


2 Answers

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

{{ formset.non_form_errors.as_ul }} <table id="formset" class="form"> {% for form in formset.forms %}   {% if forloop.first %}   <thead><tr>     {% for field in form.visible_fields %}     <th>{{ field.label|capfirst }}</th>     {% endfor %}   </tr></thead>   {% endif %}   <tr class="{% cycle row1 row2 %}">   {% for field in form.visible_fields %}     <td>     {# Include the hidden fields in the form #}     {% if forloop.first %}       {% for hidden in form.hidden_fields %}       {{ hidden }}       {% endfor %}     {% endif %}       {{ field.errors.as_ul }}       {{ field }}     </td>   {% endfor %}   </tr> {% endfor %} </table> 
like image 66
Dave Avatar answered Sep 19 '22 08:09

Dave


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> 
like image 44
JAL Avatar answered Sep 20 '22 08:09

JAL