2017-01-16 72 views
0

我已经遍地搜索,但我无法解决它。我的页面中有一个formset(底部的表格)。当我使用ajax按下保存按钮时,主窗体和formset需要保存。 POST请求已发送但出现错误。管理表单数据丢失或已被篡改验证表格

ERROR “POST /新/ 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('/') 

HTML

<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); 
    } 

我到底做错了什么?任何帮助将不胜感激。 Multiple formsets

编辑 我没有改变表单集数。我的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" /> 
+0

你确定你传入的csrf标记是正确的吗? – Bono

+0

您是否使用javascript来更改formset中窗体的数量? – schwobaseggl

+0

如果您提交没有Ajax的表单,它会工作吗?它看起来好像你的'data'包含csrf标记,但没有别的。 – Alasdair

回答

1

如果在发送到浏览器后表单数量发生了变化,formset会引发此确切错误。该表单使用management_form中名称form-TOTAL_FORMS的隐藏输入字段来跟踪该号码。从docs

It is used to keep track of how many form instances are being displayed. If you are adding new forms via JavaScript, you should increment the count fields in this form as well.