Django管理表单缺失或被篡改 - 管理表单在模板中

时间:2016-11-02 07:50:15

标签: python django forms templates formsets

我有一个包含大量表单的模板,所有表单都包含在一个表单元素中。我有MultiForm,它由4个常规表单和两个表单集组成。已重写表单集以使用自定义formset类。 我在模板中呈现管理表单,并可以在帖子中看到相关信息。

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

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

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)

Forms.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')

1 个答案:

答案 0 :(得分:0)

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

问题是如果选中了某个复选框,我只显示了表单集,并且管理表单位于隐藏区域。我将它移出了隐藏的div,它完美地工作了。