django错误管理表单丢失或已被篡改

时间:2018-07-30 15:48:21

标签: django django-forms django-templates

我无法解决此错误:

ValidationError:ManagementForm数据丢失或已被篡改

更新: 如果删除任何表单集(sr_formset,ttt_formset,gc_formset,pc_formset,gpm_formset)的注释,则会收到错误消息。 我的看法:

def manage_score(request,pk):
ScoreFormSetSR = formset_factory(ScoreForm,min_num=12,max_num=12)
ScoreFormSetTTT = formset_factory(ScoreForm,min_num=12,max_num=12)
ScoreFormSetGC = formset_factory(ScoreForm,min_num=12,max_num=12)
ScoreFormSetPC = formset_factory(ScoreForm,min_num=6,max_num=6)
ScoreFormSetGPM = formset_factory(ScoreForm,min_num=6,max_num=6)
ScoreFormSetHC = formset_factory(ScoreForm,min_num=8,max_num=8)
ScoreFormSet1C = formset_factory(ScoreForm,min_num=8,max_num=8)
stage = DailyStage.objects.get(pk=pk)



if request.method == 'POST':


    if stage.tipo=='SR':
        formsetHC_list = []
        formset1C_list = []
        for n in range (stage.hc):
            formsetHC_list.append(ScoreFormSetHC)
        for k in range (stage.UNOc):
            formset1C_list.append(ScoreFormSet1C)

        #sr_formset = ScoreFormSetSR(request.POST, request.FILES, prefix='SR')
        #gc_formset = ScoreFormSetGC(request.POST, request.FILES, prefix='GC')
        #pc_formset = ScoreFormSetPC(request.POST, request.FILES, prefix='PC')
        #gpm_formset = ScoreFormSetGPM(request.POST, request.FILES, prefix='GPM')


        return render(request, 'game/score.html', {#'sr_formset': sr_formset,
                                            #'gc_formset': gc_formset,
                                            #'pc_formset': pc_formset,
                                            #'gpm_formset': gpm_formset,
                                            'formset1C_list':formset1C_list,
                                            'formsetHC_list':formsetHC_list,
                                            })

    if stage.tipo=='TTT':
        formsetHC_list = []
        formset1C_list = []
        for n in range (stage.hc):
            formsetHC_list.append(ScoreFormSetHC)
        for k in range (stage.UNOc):
            formset1C_list.append(ScoreFormSet1C)

        #ttt_formset = ScoreFormSetTTT(request.POST, request.FILES, prefix='TTT')
        #gc_formset = ScoreFormSetGC(request.POST, request.FILES, prefix='GC')
        #pc_formset = ScoreFormSetPC(request.POST, request.FILES, prefix='PC')
        #gpm_formset = ScoreFormSetGPM(request.POST, request.FILES, prefix='GPM')
        return render(request, 'game/score.html', {
                                            #'ttt_formset': ttt_formset,
                                            #'gc_formset': gc_formset,
                                            #'pc_formset': pc_formset,
                                            #'gpm_formset': gpm_formset,
                                            'formset1C_list':formset1C_list,
                                            'formsetHC_list':formsetHC_list
                                            })

else:
    print('----------------------------------------------------')

    if stage.tipo=='SR':
        print(stage.tipo)
        formsetHC_list = []
        formset1C_list = []
        for n in range (stage.hc):
            formsetHC_list.append(ScoreFormSetHC)
        for k in range (stage.UNOc):
            formset1C_list.append(ScoreFormSet1C)
        sr_formset = ScoreFormSetSR(request.POST, request.FILES, prefix='SR')
        #gc_formset = ScoreFormSetGC(request.POST)
        #pc_formset = ScoreFormSetPC(request.POST, request.FILES, prefix='PC')
        #gpm_formset = ScoreFormSetGPM(request.POST, request.FILES, prefix='GPM')
        return render(request, 'game/score.html', {
                                            #'sr_formset': sr_formset,
                                            #'gc_formset': gc_formset,
                                            #'pc_formset': pc_formset,
                                            #'gpm_formset': gpm_formset,
                                            'formset1C_list':formset1C_list,
                                            'formsetHC_list':formsetHC_list,
                                            #'sr_formset': sr_formset,
                                            })


    if stage.tipo=='TTT':
        formsetHC_list = []
        formset1C_list = []
        for n in range (stage.hc):
            formsetHC_list.append(ScoreFormSetHC)
        for k in range (stage.UNOc):
            formset1C_list.append(ScoreFormSet1C)
        #ttt_formset = ScoreFormSetTTT(request.POST, request.FILES, prefix='TTT')
        #gc_formset = ScoreFormSetGC(request.POST, request.FILES, prefix='GC')
        #pc_formset = ScoreFormSetPC(request.POST, request.FILES, prefix='PC')
        #gpm_formset = ScoreFormSetGPM(request.POST, request.FILES, prefix='GPM')
        return render(request, 'game/score.html', {#'ttt_formset': ttt_formset,
                                            #'gc_formset': gc_formset,
                                            #'pc_formset': pc_formset,
                                            #'gpm_formset': gpm_formset,
                                            'formset1C_list':formset1C_list,
                                            'formsetHC_list':formsetHC_list,
                                            })

我的模板:

<h1>Score page</h1>
<form method="POST" >
{% csrf_token %}


<div style="border-style: solid;width:50%;float:left;">
    <h3>FORMSET HC LIST</h3>
    {% for hc_formset in formsetHC_list %}
        {{ hc_formset.management_form }}
        <h3>--------------</h3>
        <table>
            {% for form in hc_formset %}
            {{ form }}
            {% endfor %} 
        </table>
    {% endfor %}
</div>
 <div style="border-style: solid;width:50%;float:left;">
    <h3>FORMSET 1C LIST</h3>
    {% for unc_formset in formset1C_list %}
        {{ unc_formset.management_form }}
        <h3>--------------</h3>
        <table>
            {% for form in unc_formset %}
            {{ form }}
            {% endfor %} 
        </table>
    {% endfor %}
</div> 
<div style="border-style: solid;width:50%;float:left;">
    <h3>SR CLASSIFICATION</h3>
    {{ sr_formset.management_form }}
    <table>
        {% for form in sr_formset %}
        {{ form }}
        {% endfor %} 
    </table>
</div>

<div style="border-style: solid;width:50%;float:left;">
        <h3>TTT CLASSIFICATION</h3>
    {{ ttt_formset.management_form }}
    <table>
        {% for form in ttt_formset %}
        {{ form }}
        {% endfor %} 
    </table>
</div>
<div style="border-style: solid;width:50%;float:left;">
        <h3>GENERAL CLASSIFICATION</h3>
    {{ gc_formset.management_form }}
    <table>
        {% for form in gc_formset %}
        {{ form }}
        {% endfor %} 
    </table>
</div>
<div style="border-style: solid;width:50%;float:left;">
        <h3>POINTS CLASSIFICATION</h3>
    {{ pc_formset.management_form }}
    <table>
        {% for form in pc_formset %}
        {{ form }}
        {% endfor %} 
    </table>
</div>
<div style="border-style: solid;width:50%;float:left;">
        <h3>GPM CLASSIFICATION</h3>
    {{ gpm_formset.management_form }}
    <table>
        {% for form in gpm_formset %}
        {{ form }}
        {% endfor %} 
    </table>
</div>

,这是追溯的屏幕截图 screenshot Traceback

1 个答案:

答案 0 :(得分:1)

即使您不打算使用该表单集,也必须为传递到模板中的每个表单集调用{{ <some_formset>.management_form }}。否则,这就是错误“丢失”的原因。