Django表单中的水平(每行)表单 [英] Horizontal (per-row) forms in a Django formset

查看:134
本文介绍了Django表单中的水平(每行)表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django的方式是水平呈现一个表单,即每个表单一行? as_table方法垂直生成多个表单(带标签)。我需要表格行中的表单域(每个表单一行),标签应该在顶部。我什么也看不到。这是否因为某些原因而不鼓励?

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?

我应该澄清一下,我实际上希望一个表,因为一个UI表小部件,我将使用。那个表应该有。

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 .

所以我想要的结构是:

<table>
  <thead>
     <tr><th>column1</th><th>column2</th></tr>
  </thead>
  <tbody>
    <tr><td>form1.value1</td><td>form1.value2</td></tr>
...
  </tbody>
</table>


推荐答案

你可能想尝试这样的一个 http://www.djangosnippets.org/snippets/1442/

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>

这篇关于Django表单中的水平(每行)表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆