Django表单生成两次

时间:2016-04-14 18:30:36

标签: python html django forms

我在一些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>

forms.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)

1 个答案:

答案 0 :(得分:4)

{{ form.as_p }}是以渲染形式构建的django。你也手动渲染了同样的东西,所以这样做可能是不必要的。

请参阅django doc render form in template