我想顯示一行,其中包含數據庫中每個項目的標籤,文本字段和複選框。我設法做到這一點,除了在新行中的複選框。我wan't:Django中同一行上的多個字段
<tr>
<td>Label</td>
<td>Input</td>
<td>Checkbox</td>
<tr>
但我得到的是:
<tr>
<td>Label</td>
<td>Input</td>
</tr>
<tr>
<td>Checkbox</td>
</tr>
任何人知道如何做到這一點?
編輯:
要生成我做的形式:
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
attribute_form = AttributeForm(extra=user)
return render_to_response('user.html', {'username': username, 'attribute_form': attribute_form})
模板(user.html)
<form action="" method="post">
<table>{{ attribute_form.as_table }}</table>
<input type="submit" value="Save attributes">
</form>
編輯2:
我的模板弄成這樣:
<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>
你是如何產生這個代碼?顯示使用實際的模板或表單代碼。這是爲管理員還是爲自定義應用程序? – 2012-03-29 07:34:33
你能向我們展示Django爲此使用的模板嗎? – 2012-03-29 07:35:00
更新了我使用的代碼。 – olofom 2012-03-29 07:51:21