Python ManagementForm 数据丢失或被篡改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25331500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:03:50  来源:igfitidea点击:

ManagementForm data is missing or has been tampered with

pythondjangodjango-formsdjango-templatesdjango-views

提问by user2401997

I am trying to create a formset to save records in a go. But, I keep getting the error when I submit my form. And if possible please as tell me how should I save my batch of records.

我正在尝试创建一个表单集来保存记录。但是,当我提交表单时,我不断收到错误消息。如果可能,请告诉我应该如何保存我的一批记录。

My views.py:

我的意见.py:

def weekly_progress(request):
    ProgressFormSet = formset_factory(WeeklyProgressReportForm, extra=16)
    formset = ProgressFormSet(request.POST or None)

    if formset.is_valid():
        for f in formset:
           print(f)

    return render(request, "progress/progressentry.html", {'formset' : formset})

My forms.py

我的表格.py

class WeeklyProgressReportForm(forms.ModelForm):
    class Meta:
       model = WeeklyProgressReport
       fields = ('target_date', 'this_date', 'pkgno', 'slno', 'description', 'unit', 'receipt_this_week', 'issue_this_week', 'erection_this_week')
       widgets = {
        'target_date': forms.DateInput(attrs={'class': 'form-control', 'readonly': 'readonly'}),
        'this_date': forms.DateInput(attrs={'class': 'form-control', 'readonly': 'readonly'}),
        'pkgno': forms.TextInput(attrs={'class': 'form-control', 'readonly': 'readonly'}),
        'slno': forms.NumberInput(attrs={'class': 'form-control', 'readonly': 'readonly'}),
        'description': forms.Textarea(attrs={'class': 'form-control', 'rows': 1, 'readonly': 'readonly'}),
        'unit': forms.TextInput(attrs={'class': 'form-control', 'readonly': 'readonly'}),
        'receipt_this_week': forms.NumberInput(attrs={'class': 'form-control', 'step': 0.01}),
        'issue_this_week': forms.NumberInput(attrs={'class': 'form-control', 'step': 0.01}),
        'erection_this_week': forms.NumberInput(attrs={'class': 'form-control', 'step': 0.01, 'readonly': 'readonly'}),
       }

My template:

我的模板:

<form id="contractor-form" method="post" action="">
    {% csrf_token %}

    <!-- First Row -->
    <div class="col-lg-6">
        <div class="panel panel-primary">
            <div class="panel-heading">Select Your Package</div>
            <div class="panel-body">
                <div class="col-lg-4">                        
                    <h4><label class="label label-primary">Package Number</label></h4>
                </div>
                <div class="col-lg-4">
                    <select id="pkgno-select" class="form-control">                                
                        <option value="12 (BRP)">12 (BRP)</option>
                        <option value="13 (BRP)">13 (BRP)</option>
                        <option value="13 (DHB)">13 (DHB)</option>
                        <option value="14 (DHB)">14 (DHB)</option>
                    </select>
                </div>
                <div class="col-lg-4">                        
                        <button type="button" id="date-edit" class="btn btn-warning">Edit Date</button>                        
                </div>
            </div>
        </div>
    </div>


    <!-- Second Row -->
    <div class="col-lg-12">
        <div class="panel panel-primary">
            <div class="panel-heading">Quantities</div>
            <div class="panel-body">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>Target Date</th>
                            <th>This Date</th>
                            <th>Pkg Number</th>
                            <th>Sl Number</th>
                            <th>Description</th>
                            <th>Unit</th>
                            <th>Receipt This Week</th>
                            <th>Issue This Week</th>
                            <th>Erection This Week</th>                                
                        </tr>
                    </thead>
                    <tbody>
                        {% for form in formset %}
                        <tr>
                           <td>{{ form.target_date }}</td>
                           <td>{{ form.this_date }}</td>
                           <td>{{ form.pkgno }}</td>
                           <td>{{ form.slno }}</td>
                           <td>{{ form.description }}</td>
                           <td>{{ form.unit }}</td>
                           <td>{{ form.receipt_this_week }}</td>
                           <td>{{ form.issue_this_week }}</td>
                           <td>{{ form.erection_this_week }}</td>                               
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Submit Button -->
    <div class="well well-lg">
        <button type="submit" class="btn btn-success btn-lg btn-block">Save</button>
    </div>
</form>

My models.py

我的模型.py

class WeeklyProgressReport(models.Model):
   target_date = models.DateField()
   this_date = models.DateField()
   pkgno = models.CharField(max_length=10)
   slno = models.IntegerField(max_length=2)
   description = models.CharField(max_length=50)
   unit = models.CharField(max_length=5)
   target_quantity = models.DecimalField(max_digits=12, decimal_places=2, blank=True, default=0)
   receipt_previous = models.DecimalField(max_digits=12, decimal_places=2, blank=True, default=0)
   receipt_this_week = models.DecimalField(max_digits=12, decimal_places=2, blank=True, default=0)
   issue_previous = models.DecimalField(max_digits=12, decimal_places=2, blank=True, default=0)
   issue_this_week = models.DecimalField(max_digits=12, decimal_places=2, blank=True, default=0)
   erection_previous = models.DecimalField(max_digits=12, decimal_places=2, blank=True, default=0)
   erection_this_week = models.DecimalField(max_digits=12, decimal_places=2, blank=True, default=0)    

   def __unicode__(self):
       return self.pkgno

采纳答案by Peter DeGlopper

You have to render the management form in your template. The docsexplain why and how; some selected quotes:

您必须在模板中呈现管理表单。该文档解释为何以及如何; 一些精选的报价:

This form is used by the formset to manage the collection of forms contained in the formset. If you don't provide this management data, an exception will be raised[.]

The management form is available as an attribute of the formset itself. When rendering a formset in a template, you can include all the management data by rendering {{ my_formset.management_form }} (substituting the name of your formset as appropriate).

表单集使用此表单来管理表单集中包含的表单集合。如果您不提供此管理数据,则会引发异常[.]

管理表单可用作表单集本身的属性。在模板中呈现表单集时,您可以通过呈现 {{ my_formset.management_form }}(根据需要替换表单集的名称)来包含所有管理数据。

回答by yahya

write {{ formset.management_form }}above your {% for form in formset %}

{{ formset.management_form }}在你的上面{% for form in formset %}

like this

像这样

{{ formset.management_form }}
{% for form in formset %}