gpt4 book ai didi

python - ModelFormSet 上的额外表单

转载 作者:行者123 更新时间:2023-11-30 22:20:50 25 4
gpt4 key购买 nike

对于我的 Django 项目,我在模板中渲染模型表单集 election_formset = modelformset_factory(Election, except=('Complete',), formset=BaseElectionFormSet):

<form method="post" action="">
{{ formset.management_form }}
{% for form in formset %}
<div class='card'>
<div class='card-body w-75 mx-auto'>

<div class='row'>
<div class='col-6 text-center'>
<p>Name<br>{{form.Name}}</p>
</div>
<div class='col-6 text-center'>
<p>Videos<br>{{form.FlipGrid}}</p>
</div>
</div>
<div class='row'>
<div class='col-12 text-center'>
<p>Description<br>{{form.Description}}</p>
</div>
</div>
<div class='row'>
<div class='col-6 text-center'>
<p>Allow registration: {{form.CandidateReg}}</p>
</div>
<div class='col-6 text-center'>
<p>Allow voting: {{form.VotingOpen}}</p>
</div>
</div>
</div>
</div>
{% endfor %}
</form>

当表单集呈现时,表单末尾会显示一个额外的空白表单。我只希望表单显示现有记录的实例。为什么会出现额外的空白表单集?如何防止出现这种情况?

最佳答案

试试这个

election_formset = modelformset_factory(
Election,
exclude=('Complete',),
formset=BaseElectionFormSet,
extra=0
)

extra 关键字默认为 1,否则(请参阅 docs ):

def modelformset_factory(model, form=ModelForm, formfield_callback=None,
formset=BaseModelFormSet, extra=1, can_delete=False,
can_order=False, max_num=None, fields=None, exclude=None,
widgets=None, validate_max=False, localized_fields=None,
labels=None, help_texts=None, error_messages=None,
min_num=None, validate_min=False, field_classes=None):

关于python - ModelFormSet 上的额外表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48765285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com