我到处找遍了,但没能解决。我的页面上有一个表格集(表格在底部)。当我使用ajax按“保存”按钮时,需要保存主窗体和窗体集。已发送POST请求,但存在错误。
错误"POST /new/ HTTP/1.1“500 59
ValidationError: U‘’ManagementForm数据丢失或已被篡改“”
Views.py
def master_detail_new(request):
if request.method == 'GET':
author = TmpPlInvoice()
author_form = TmpForm(instance=author)
BookFormSet = inlineformset_factory(TmpPlInvoice, TmpPlInvoicedet,
exclude=('emp_id', 'voucher', 'lineitem', 'id',),
form=TmpFormDetForm, )
formset = BookFormSet(instance=author)
return render(request, 'main.html',
{'form': author_form, 'formset': formset, 'formtotal': totalform, 'postform': postform},
)
elif request.method == 'POST':
def get_new_voucher_id():
temp_vid = TmpPlInvoice.objects.order_by().values_list("voucher_id", flat=True).distinct()
if not temp_vid:
voucher_id = str(1).zfill(4)
else:
voucher_id = str(int(max(temp_vid)) + 1).zfill(4)
return voucher_id
author_form = TmpForm()
author = TmpPlInvoice()
BookFormSet = inlineformset_factory(TmpPlInvoice, TmpPlInvoicedet, exclude=('emp_id', 'voucher', 'lineitem', 'id',),
form=TmpFormDetForm, extra=2)
formset = BookFormSet(instance=author)
voucher_id = get_new_voucher_id()
author = TmpForm(request.POST)
if author.is_valid():
created_author = author.save(commit=False)
created_author.voucher_id = voucher_id
created_author.save()
formset = BookFormSet(request.POST, instance=created_author)
if formset.is_valid():
formset.save()
return HttpResponseRedirect('/')
<div class="x_content">
{{ formset.management_form }}
{{ formset.non_form_errors.as_ul }}
<table class="table table-striped responsive-utilities jambo_table bulk_action form"
id="formset" style="background-color:#d0ffff;">
<thead style="background-color:#9df0e0;;color: #73879C">
{% for form in formset.forms %}
{% if forloop.first %}
<thead>
<tr class="headings">
{% for field in form.visible_fields %}
<th>{{ field.label|capfirst }}</th>
{% endfor %}
</tr>
</thead>
{% endif %}
Javascript发送数据
$("#save").click(function() {
$.ajax({
type:'POST',
url:'/new/',
data:{
csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val()
},
success:searchSuccess,
dataType: 'html'
});
});
function searchSuccess(data, textStatus, jqXHR)
{
$('#myForm').html(data);
}
我做错什么了?任何帮助都将不胜感激。多模板集
编辑I不更改窗体集的数量。我的CSRF运转良好。而且,如果没有ajax,我也会遇到同样的问题。
<input id="id_tmpplinvoicedet_set-TOTAL_FORMS" name="tmpplinvoicedet_set-TOTAL_FORMS" type="hidden" value="3" />
<input id="id_tmpplinvoicedet_set-INITIAL_FORMS" name="tmpplinvoicedet_set-INITIAL_FORMS" type="hidden" value="0" />
<input id="id_tmpplinvoicedet_set-MIN_NUM_FORMS" name="tmpplinvoicedet_set-MIN_NUM_FORMS" type="hidden" value="0" />
<input id="id_tmpplinvoicedet_set-MAX_NUM_FORMS" name="tmpplinvoicedet_set-MAX_NUM_FORMS" type="hidden" value="1000" />
发布于 2017-01-16 09:37:15
如果表单数量在发送到浏览器后发生了更改,则此错误将由窗体集引发。表单集使用management_form
中名为management_form
的隐藏输入字段来跟踪该数字。来自文档
它用于跟踪显示了多少表单实例。如果要通过JavaScript添加新表单,也应该在此表单中增加计数字段。
发布于 2018-08-19 10:04:44
这就是我如何更改每个表单的TOTAL_FORMS。注意*当向页面动态添加表单时,我传入一个表单作为所有HTML和正则表达式的ID。
其中一个是我调用的add按钮:
addForm: function () {
this.count++
let form_count = this.count
form_count++
let formID = 'id_form-' + this.count
incremented_form = this.vue_form.replace(/form-\d/g, 'form-' + this.count)
this.formList.push(incremented_form)
this.$nextTick(() => {
let total_forms = document.getElementsByName('form-TOTAL_FORMS').forEach
(function (ele, idx) {
ele.value = form_count
})
})
},
这将是所有TOTAL_FORMS的全部1。从我很少的经验来看,django看最后一个表单,看看它提交的表单数量是否正确。因此,您可能不必更新
https://stackoverflow.com/questions/41681462
复制相似问题