Djang中同一行上的多个字段

2024-10-03 17:25:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我想为数据库中的每个项目显示一行标签、文本字段和复选框。除了在新行上的复选框之外,我已经成功地做到了这一点。我不想:

<tr>
    <td>Label</td>
    <td>Input</td>
    <td>Checkbox</td>
<tr>

但我得到的只是:

^{pr2}$

有人知道怎么做吗?在

编辑:

要生成表单,我需要:

表单.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)

视图.py

attribute_form = AttributeForm(extra=user)
return render_to_response('user.html', {'username': username, 'attribute_form': attribute_form})

模板(用户.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>

Tags: keyselfformfieldtableattributeformsextra