2011-03-09 48 views
0

誰能告訴我,爲什麼下面的視圖不會接你的POST請求:查看不會回暖後

# Loads all the latest phone numbers for the models.py file 
def client_phones_form_view(request, clientKEY): 
    try: 
     i_clientKEY = int(clientKEY) 
    except ValueError: 
     raise Http404() 
    phones = [] 
    # Populates a list with the latest phone numbers for ALL types of phone 
    for k, v in PHONE_CHOICES: 
     try: 
      phones.append(ClientPhone.objects.filter(client=i_clientKEY, phone_type=k).latest('id')) 
     except ClientPhone.DoesNotExist: 
      pass 
    if request.method == 'POST': 
     formPhone = ClientPhoneForm(request.POST) 
     if formPhone.is_valid() and formPhone.number.clean(formPhone.number): 
      c = Client.objects.get(id=i_clientKEY) 
      formPhone.CustomSave(c, request.user) 
      return render_to_response(...) 
     else: 
      return render_to_response(...) 
    else: 
     formPhone = ClientPhoneForm() 
     return render_to_response(...) 

我知道當我提交它的重裝形式,但它總是重新加載底部render_to_response

編輯:

的JavaScript看起來是這樣的:

$("#newPhoneDialog").dialog({ 
    autoOpen: false, 
    height: 200, 
    width: 350, 
    modal: true, 
    buttons: { 
     "Add New Phone Number": function() { 
      var bValid = true; 
      //alert($('#id_number').val()+" - "+$('#id_phone_type').val()); 

      bValid = bValid && checkNumberLength(phoneNumber, "the phone number", 11, 15); 

      bValid = bValid && checkNumberRegexp(phoneNumber, /^([0-9])|(\t)+$/, "The phone number may only consist of numbers and spaces"); 

      if (bValid) { 
       $('.error').hide(); // hide the error div 
       $('.success').hide(); // hide the success div 
       $('.info').hide(); // hide the information div 
       $.ajax({ // create an AJAX call... 
        data: $('#newPhoneForm').serialize(), // get the form data 
        type: 'POST', // GET or POST 
        url: 'client/form/phones/1/', // the file to call 
        success: function(response) { // on success.. 
         $('#clientPhonesForm').html(response); // update the main phones DIV 
        } 
       }); 
       $(this).dialog("close"); 
       //return false; // cancel original event to prevent form submitting 
      } 
     }, 
     Cancel: function() { 
      // reloads the phone div because re-selecting the first item in the options doesn't work 
      $('#clientPhonesForm').load('{% url client.views.client_phones_form_view clientKEY %}'); 
      $(this).dialog("close"); 
     } 
    }, 
    close: function() { 
     // reloads the phone div because re-selecting the first item in the options doesn't work 
     //$('#clientPhonesForm').load('{% url client.views.client_phones_form_view clientKEY %}'); 
    } 
}); 

和HTML這樣的:

<div id="newPhoneDialog" title="Create New Phone Number"> 
     Please enter a valid phone number: 
    <div id="container"> 
     <form action="{% url client.views.client_phones_form_view clientKEY %}" method="POST" id="newPhoneForm"> 
      <table> 
      {% for field in formPhone %} 
      <tr><td>{{ field.label_tag }}</td><td>{{ field }}</td></tr> 
      {% endfor %} 
      </table> 
     </form> 
    </div> 
</div> 

編輯

也許,如果它說這個問題會更好,「我如何(jQuery的AJAX)提交表單時單擊按鈕時」

+0

您能告訴我在開發服務器或登錄嘗試中有多少請求會通過嗎?當你點擊按鈕時,會發生什麼?開發服務器是否註冊POST,然後用GET重新加載頁面?它是否註冊了一個GET並且頁面沒有重新加載? – 2011-03-09 15:54:16

+0

當我註釋掉'formPhone.number.clean(formPhone.number)'時,dev服務器只顯示POST請求 – Sevenearths 2011-03-09 16:47:57

+0

'一切似乎都正常。 'formPhone.number'使用來自http://djangosnippets.org/snippets/1207/ – Sevenearths 2011-03-09 16:49:53

回答

0

以下似乎是答案。是否正確我不知道:

# Loads all the latest phone numbers for the models.py file 
def client_phones_form_view(request, clientKEY): 
    try: 
     i_clientKEY = int(clientKEY) 
    except ValueError: 
     raise Http404() 
    phones = [] 
    # Populates a list with the latest phone numbers for ALL types of phone 
    for k, v in PHONE_CHOICES: 
     try: 
      phones.append(ClientPhone.objects.filter(...).latest('id')) 
     except ClientPhone.DoesNotExist: 
      pass 
    if request.method == 'POST': 
     formPhone = ClientPhoneForm(request.POST) 
     if formPhone.is_valid(): 
      try: 
       n = formPhone.cleaned_data['number'] 
      except ValidationError: 
       return render_to_response(...) 
      c = Client.objects.get(id=i_clientKEY) 
      formPhone.CustomNumberSave(c, n, request.user) 
      return render_to_response(...) 
     else: 
      return render_to_response(...) 

    else: 
     formPhone = ClientPhoneForm() 
     return render_to_response(...) 
2

這是一個防彈條件:if request.method == 'POST'所以我會開始尋找你爲什麼不生成POST請求,而不是視圖。

你能確定你是從你的表單發佈嗎?

<form method="post">

是開發服務器註冊POST請求?應該說"GET /...""POST /..."


更新:行,所以這是一個Ajax請求的形式。最近有另一個人有類似的問題,因爲AJAX請求觸發了原始表單作爲GET請求。

我看到你註釋掉了return false - 這可能是問題嗎?其實從來沒有想過,我不認爲對話按鈕會干擾。

更重要的是:什麼是開發服務器錄音? POST然後GET?單個GET?

+0

我嘗試了下面的http://pastebin.com/few79mwX,它仍然使用最後的'render_to_response': ( – Sevenearths 2011-03-09 16:34:03

+0

通過輸出'request.method'我發現它是一種POST方法 – Sevenearths 2011-03-09 16:34:51