- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试创建一个表单集来即时保存记录。但是,当我提交表单时,我总是收到错误消息。如果可能的话,请告诉我应该如何保存我的批记录。
我的观点.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})
我的表格.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'}),
}
我的模板:
<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>
我的模型.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
最佳答案
您必须在模板中呈现管理表单。 docs解释原因和方式;一些精选的引述:
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).
关于python - ManagementForm 数据丢失或被篡改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25331500/
我正在尝试创建一个表单集来即时保存记录。但是,当我提交表单时,我总是收到错误消息。如果可能的话,请告诉我应该如何保存我的批记录。 我的观点.py: def weekly_progress(reques
在views.py中创建表单集: ffact = formset_factory(Form,extra=somenum])) fset = ffact(prefix='pfix') views.py
有人知道为什么在我使用 Formset Prefix 时会引发 ManagementForm Data is Missing 吗? 从外壳 >>> from django import forms >
我不断收到错误: [u'ManagementForm data is missing or has been tampered with'] 我也想不通为什么。这是我的观点: def Creat
我一直在研究关于这个问题的每一个问题,但似乎找不到解决方案。 我正在尝试允许用户提交多个对象并使用带有 2 个外键的表单集保存到数据库。 我可以获取表单以将该数据保存到数据库中,但由于 Managem
出于某种原因,我的表单集在提交后无法验证。关于这可能发生的方式有什么想法吗? #模型.py class Department(models.Model): department = models.
晚上好, 我在处理脆表单 inlineformset 时遇到了麻烦。我已按照以下指南进行操作: https://github.com/timhughes/django-cbv-inline-forms
所以我环顾四周,似乎没有人遇到过我不得不导致这个看似常见的错误的相同问题。我在我的 html 中呈现一些表单,如下所示: {{ tags_formset.management_form }} ..
我有一个 models.py 类如下 class Educational_Qualification(models.Model): user = models.ForeignKey(User)
我是 Django 的新手,所以这可能是一个简单的问题。我有 2 个 modelForms,其中有一个 ForeignKey 到另一个。我的主要目标是保存具有疾病 (FK) 链接的指标,这样对于特定疾
我是一名优秀的程序员,十分优秀!