Django表单生成twi

2024-04-18 22:14:08 发布

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

我在用一些css包装我的简单django申请表,并意识到我的表单被生成了两次(下图)enter image description here

我希望它是一个表的形式,正如你在下面的代码中看到的,我显式地将表单包装在一个表中。然而,表格出现在表格上方,然后又出现在表格中。我重新检查了我的代码,但似乎无法找出问题所在

相关代码-生成表单的模板

<a href="/ad_accounts"> All Accounts </a>
<br />
<table class="table table-bordered table-striped">
    <form action="." method="POST">{% csrf_token %}
    <thead>
        <th>Attribute</th>
        <th>Value</th>
    </thead>
    <tr>
        <td>Title</td>
        <td>
            {{ form.title }}
        </td>
    </tr>
    <tr>
        <td>Objective</td>
        <td><select class="select2_single form-control" tabindex="-1">
            <option value={{ form.objective }}</option>
                </select></td>
    </tr>
    {{ form.as_p }}
    <tr>
        <td><input class="btn btn-success" type="submit" value="Submit"/></td>
    </tr>
    </form>

表单.py

from django import forms

class CampaignForm(forms.Form):
    """ Form for creating new Campaign """

    def __init__(self, *args, **kwargs):
        objectives = kwargs.get('objectives')
        if objectives:
            kwargs.pop('objectives')
        else:
            objectives = list()
        super(CampaignForm, self).__init__(*args, **kwargs)
        self.fields['title'] = forms.CharField(max_length=50)
        self.fields['objective'] = forms.ChoiceField(choices=objectives)

Tags: django代码selfform表单tableformstr