kmt*_*kmt 15 django django-forms
什么是Django水平呈现formset的方式,即每个表单一行?as_table方法垂直生成多个表单(带有标签).我需要表格行中的表单字段(每个表单一行),标签应该在顶部.我没有看到任何开箱即用的东西.出于某种原因,这是否气馁?
我应该澄清我实际上想要一个表,因为我将使用的UI表小部件.那个表应该有标签.
所以我想要的结构是:
<table>
<thead>
<tr><th>column1</th><th>column2</th></tr>
</thead>
<tbody>
<tr><td>form1.value1</td><td>form1.value2</td></tr>
...
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
Dav*_*ave 28
你可能想尝试这样的东西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>
Run Code Online (Sandbox Code Playgroud)
我建议使用form.as_ulCSS来设置它并将其设置为一行.ul li { display: inline; }如果您不想以这种方式影响所有UL,您可以使用或当然替换类或ID.
这是Django文档的相关部分:http://docs.djangoproject.com/en/dev/topics/forms/#displaying-a-form-using-a-template
编辑:为了满足您对表格的需求,您希望做这样的事情...再编辑一些.
将所有这些表单放在表中并且仍然具有有效的HTML是很困难的.表单元素可以围绕一个表格,或者在<td>... 内部...虽然这可能仍然有效.
<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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14561 次 |
| 最近记录: |