繁体   English   中英

Django中同一行上的多个字段

[英]Multiple fields on the same row in 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>

.as_table在单独的表行中呈现每个表单字段。 您应该手动渲染表单

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM