我有一个复杂的表格:
<<forms.py>>
class AttributeOptionForm(forms.Form):
option_name = forms.CharField(label="Attribute Option")
class AttributeForm(forms.Form):
attr_name = forms.CharField(max_length=100, label="Attribute Name")
attr_options_list = [AttributeOptionForm(), AttributeOptionForm()]
class ProjectForm(forms.Form):
name = forms.CharField(max_length=250, label="Name")
attr_form_list = [AttributeForm()]ProjectFrom至少有一个AttributeForm (可以在运行时增长),每个AttributeForm至少有两个AttributeOptionForm (运行时也可能增长)。您可能会认为任何AttributeForm都是一个包含多个答案(AttributeOptionForm)的问题,我希望用户填写这个问题。
这就是我展示ProjectForm的方式。
<<project_form.html>>
<form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<span class="text-danger small">{{ form.name.errors }}</span>
</div>
<label class="control-label col-sm-2">{{ form.name.label_tag }}</label>
<div class="col-sm-9">{{ form.name }}</div>
</div>
{% for attr_form in form.attr_form_list %}
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<span class="text-danger small">{{ attr_form.att_name.errors }}</span>
</div>
<label class="control-label col-sm-2">{{ attr_form.attr_name.label_tag }}</label>
<div class="col-sm-9">{{ attr_form.attr_name }}</div>
{% for option in attr_form.attr_options_list %}
<div class="col-sm-offset-2 col-sm-10">
<span class="text-danger small">{{ option.option_name.errors }}</span>
</div>
<label class="control-label col-sm-2">{{ option.option_name.label_tag }}</label>
<div class="col-sm-9">{{ option.option_name }}</div>
{% endfor %}
</div>
{% endfor %}
</form>此外,表单中还有一个'add_attribute_option‘按钮(每个属性)、'add_attribute’按钮和'submit‘按钮。
谢谢!
发布于 2017-12-01 08:36:05
Nicole Harris有一个很棒的教程,它通过教我如何使用Formset来解决我的问题。谢谢妮可!http://whoisnicoleharris.com/2015/01/06/implementing-django-formsets.html
https://stackoverflow.com/questions/47545012
复制相似问题