[英]Multiple fields on the same row in Django
I'd like to display a row with a label, a textfield and a checkbox for each item in a database. 我想为数据库中的每个项目显示一个带有标签,文本字段和复选框的行。 I've managed to do this except for the checkbox that's on a new line. 除了新行上的复选框之外,我设法做到了这一点。 I wan't: 我想要:
<tr>
<td>Label</td>
<td>Input</td>
<td>Checkbox</td>
<tr>
But all I get is: 但我得到的只是:
<tr>
<td>Label</td>
<td>Input</td>
</tr>
<tr>
<td>Checkbox</td>
</tr>
Anyone knows how to do this? 谁知道怎么做?
EDIT: 编辑:
To generate the form I do: 要生成表单我做:
forms.py forms.py
class AttributeForm(forms.Form):
def __init__(self, *args, **kwargs):
extra = kwargs.pop('extra')
super(AttributeForm, self).__init__(*args, **kwargs)
for key in extra:
self.fields[key] = forms.CharField(label=key, initial=extra[key], required=False)
self.fields['delete_'+key] = forms.BooleanField(label='', required=False)
views.py views.py
attribute_form = AttributeForm(extra=user)
return render_to_response('user.html', {'username': username, 'attribute_form': attribute_form})
template (user.html) 模板(user.html)
<form action="" method="post">
<table>{{ attribute_form.as_table }}</table>
<input type="submit" value="Save attributes">
</form>
EDIT 2: 编辑2:
My template ended up like this: 我的模板最终是这样的:
<form action="" method="post">
<table>
<tr>
{% for field in attribute_form %}
{% cycle '<th>' '' %}{% cycle field.label_tag '' %}{% cycle '</th>' '' %}
<td>{{ field }}{{ attribute_form.field.errors }}</td>
{% if not forloop.last %}{% cycle '' '</tr><tr>' %}{% endif %}
{% endfor %}
</tr>
</table>
<input type="submit" value="Save attributes">
</form>
.as_table
renders each form field in seperate table row. .as_table
在单独的表行中呈现每个表单字段。 You should render the form manually . 您应该手动渲染表单 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.