2013-06-19 125 views
0

全部,通過ajax驗證django formset

我有一個Django視圖,內嵌formset。當我正常訪問視圖並提交時,一切正常。當我通過AJAX訪問它,不過,我得到一個驗證錯誤:

[u'ManagementForm data is missing or has been tampered with'] 

這裏是我的代碼的簡化版本:

def edit_model(request): 
    model_id = request.GET.get('i', None) 
    if model_id: 
     model = ModelClass.objects.get(pk=model_id) 
    else: 
     model = ModelClass() 

    related_model_formset_class = inlineformset_factory(ModelClass, RelatedModelClass, form=RelatedModelForm, 
                 prefix="my_related_models") 

    if request.method == "GET": 
     model_form = ModelFormClass(request.POST, instance=model) 
     related_model_formset = related_model_formset_class(request.POST, instance=model) 

     if model_form.is_valid() and related_model_formset.is_valid(): 
      model_form.save() 
      related_model_formset.save() 

      return HttpResponse("success") 
    else: 

     model_form = ModelFormClass(instance=model) 
     related_model_initial_values = [{"default_value1": 1, "default_value2": 2}, 
             {"default_value1": 1, "default_value2": 2}] 
     related_model_formset = related_model_formset_class(instance=model, initial=related_model_intial_values, 
                  extra=len(related_model_initial_values)) 

    rendered_form = django.template.loader.render_to_string("my_form.html", 
                  {"form": model_form, "formset": related_model_formset}, 
                  context_instance=RequestContext(request)) 
    return HttpResponse(rendered_form, mimetype='text/html') 

這會從一個jQuery調用另一個頁面內訪問(這具有類似的結構爲 「my_form.html」,用inline_formsets):

function edit_model(model_id) { 

    var url = "http://www.mydomain.com/edit_model/?i=" + model_id 

    var edit_model_dialog = $("<div></div>"); 
    $.ajax({ 
     url  : url, 
     type  : "GET", 
     cache  : false, 
     success : function(data) { 
      var title = "here is a form to edit the model" 
      edit_model_dialog(data); 
      edit_model_dialog.dialog({ 
       title : title, 
       modal : true, 
       dialogClass: "no-close", 
       close : function() { 
        $(this).dialog("destroy"); 
       } 
      }).dialog('open'); 
     } 
    }) 

更新:

我還沒有解決這個問題,但我已經縮小了這個問題的範圍。觸發「edit_model」函數的按鈕位於另一個頁面上,該頁面具有與按鈕創建的對話框中呈現的類型相同的類型和窗體集。當對話框中的表單上的提交被推送時,它似乎從此父頁面觸發一個POST以及對話框中的頁面。顯然,這兩個頁面上的管理數據不匹配。

所以,我想弄清楚如何防止提交對話框傳播到父頁面。

+0

在窗體中您是否包含'{{form.management_form}}? – karthikr

+0

@karthikr - 是的,我有。 – trubliphone

回答

1

我結束了改變我的JavaScript顯式發送一個適當的值,而不是依靠對話框中的窗體的默認「提交」行爲。新代碼看起來像這樣:

function edit_model(model_id) { 

    var url = "http://www.mydomain.com/edit_model/?i=" + model_id 

    var edit_model_dialog = $("<div></div>"); 
    $.ajax({ 
    url  : url, 
    type  : "GET", 
    cache  : false, 
    success : function(data) { 
     var title = "here is a form to edit the model" 
     edit_model_dialog(data); 
     edit_model_dialog.dialog({ 
     title : title, 
     modal : true, 
     dialogClass: "no-close", 
     close : function() { 
      $(this).dialog("destroy"); 
     }, 
     // HERE IS THE NEW BIT... 
     buttons : { 
      ok : function() { 
      // GET THE DATA FROM THE FORM IN THE DIALOG... 
      var form_data = $(this).find("the_form").serialize(); 
      $.ajax({ 
       url : url 
       // EXPLICITLY SEND IT AS A POST... 
       type : "POST", 
       data : form_data, 
       cache : false, 
       success : function(data) { 
       if (data == "success") { 
        $(edit_model_dialog).dialog("close"); 
       } 
       else { 
        $(edit_model_dialog).html(data); 
       } 
       } 
      }); 
      }, 
      cancel : function() { 
      $(edit_model_dialog).dialog("close"); 
      } 
     } 
     }).dialog('open'); 
    } 
    })