簡體   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