0

我有一个模板,上面有很多表单,都包含在一个表单元素中。我有一个由 4 个常规表单和两个表单集组成的 MultiForm。表单集已被覆盖以使用自定义表单集类。我在模板中渲染管理表格,并且可以在帖子中看到相关信息。

对于表单集,我初始化页面时只显示一个表单。

当我尝试提交组合表单时,出现以下错误:

ManagementForm data is missing or has been tampered with

我到处寻找答案,并阅读了大约 15 篇关于堆栈溢出的帖子,但出现了相同的错误,但似乎没有一个解决方案有帮助。

错误页面突出显示以下行:

{{ beneficiaries.management_form }}

模板:

<form class='pension_form' id='implementation_form' action="{% url "confirmation_form" %}" method="post">
 {% csrf_token %}

<ul>
{{ the_form.user_info.as_ul }}  
 </ul>
<ul>
{{ the_form.spouse_info.as_ul }}  
</ul>

 <div class='formset_container'> {{ children.management_form }}
 {% for form in children %}
    <div class='formset'><ul>{{ form.as_ul }}  </ul><a class="glyphicon glyphicon-plus"></a></div>
{% endfor %}
</div>
  <ul>
{{ the_form.employer_info.as_ul }}  
</ul>
  <ul>
<li>{{ the_form.beneficiary_general.WHO_BENEFITS }}</li>
</ul>
<div id='beneficiary_info_container' style='display:none;'>
<div class='formset_container'>
 {{ beneficiaries.management_form }}
{% for form in beneficiaries %}
<div class='formset' >
<ul>{{ form.as_ul }}</ul><a class="glyphicon glyphicon-plus"></a></div>
    {% endfor %}
</div>
<ul><li id='inheritance_order'>
{{ the_form.beneficiary_general.BENEFICIARIES_DIE.label_tag }}
{{ the_form.beneficiary_general.BENEFICIARIES_DIE }}
</li>
</ul>
</div>

<button class='btn btn-default main-btn'>{% trans "_Continue" %}
</form>

看法:

def show_confirmation_form(request):
ChildFormSet = formset_factory(ChildInfo, formset=ChildInfoFormSet, 
        extra=14, can_delete=True)
BeneficiaryFormSet = formset_factory(BeneficiaryInfo, formset=BeneficiaryInfoFormSet, 
        extra=10, can_delete=True)
multi_form_prefix = 'main_form'
child_prefix = 'children_info'
beneficiary_prefix = 'beneficiary_info'
if request.method == 'POST':
    form = ConfirmationForm(request.POST, prefix=multi_form_prefix)
    children_forms = ChildFormSet(request.POST, prefix=child_prefix)
    beneficary_forms = BeneficiaryFormSet(request.POST, 
        prefix=beneficiary_prefix)
    if form.is_valid():
            #not ready yet
            return HttpResponseRedirect('/thanks/')
else:
    form = ConfirmationForm(prefix=multi_form_prefix)
    children_forms = ChildFormSet(prefix=child_prefix)
    beneficary_forms = BeneficiaryFormSet(prefix=beneficiary_prefix)
context = {'the_form' : form, 'children' : children_forms, 
    'beneficiaries' : beneficary_forms}
return render(request, "confirmation_form.html", context)

表格.py

class BeneficiaryInfo(forms.Form):
SHEM_PRATI_MUTAV = forms.CharField(label=_("First_Name"))
SHEM_MISHPACHA_MUTAV = forms.CharField(label=_("Last_Name"))
MISPAR_ZEHUT_MUTAV = forms.IntegerField(label=_("Mispar_Zehut"))
ACHUZ_HALUKA = forms.IntegerField(label=_("Percent_Allocate"))

class BeneficiaryInfoFormSet(BaseFormSet):
def clean(self):
    """
    Adds validation to check that no two links have the same anchor or URL
    and that all links have both an anchor and URL.
    """
    if any(self.errors):
        return

    teudot_zehut = []
    distribution_total = 0

    for form in self.forms:
        if form.cleaned_data:
            teudat_zehut = form.cleaned_data['MISPAR_ZEHUT_MUTAV']
            #allow empty forms.
            if teudat_zehut:
                if teudat_zehut in teudot_zehut:
                    form.add_error(None, 'No mutavim can share teudot_zehut')
            distribution_total += int(form.cleaned_data['ACHUZ_HALUKA'])
    if distribution_total != 100:
        form.add_error(None, 'Distribution Total must be 100')
4

1 回答 1

0

如果有人遇到类似的问题:

问题是如果选中某个复选框,我只显示表单集,并且管理表单位于隐藏区域。我将它从隐藏的 div 中移出,它运行良好。

于 2017-01-22T09:57:57.067 回答