我在用一些css
包装我的简单django申请表,并意识到我的表单被生成了两次(下图)
我希望它是一个表的形式,正如你在下面的代码中看到的,我显式地将表单包装在一个表中。然而,表格出现在表格上方,然后又出现在表格中。我重新检查了我的代码,但似乎无法找出问题所在
相关代码-生成表单的模板
<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)
{{ form.as_p }}
是django内置的呈现表单的方式。您还手动渲染了相同的内容,因此这样做可能是不必要的。你知道吗关于render form in template,见django doc。你知道吗
相关问题 更多 >
编程相关推荐