2013-03-19 67 views
0

我想創建一個ajax函數,當被調用時返回信息到新創建的模態,可以幫我找出問題所在?,我得到錯誤「not found」每當我嘗試訪問有問題的URL時,我都會添加我的終端和所有相關文件的屏幕截圖。Django -Ajax請求錯誤:找不到

views.py

@require_POST() 
def form_create(request, model): 
    if request.method == "POST": 
    return HttpResponse("the model requested is") 

urls.py

url(r'^forms/(?P<model>[\W-]+)/$','.views.form_create'), 

Ajax調用在HTML模板

$.ajax({ 
    url: "/forms/"+model+"/", 
    type: "POST", 
    cache: false, 
    success: 
     function(result){ 
      $("#myModalLabel").html(result); 
      $("#companyModal").modal("show"); 
      }, 
    error: 
     function(xhr){ 
      alert("Error: " + xhr.statusText); 
      return false; 
      } 
    }); 

enter image description here

+0

你設置你的DEBUG =真? – catherine 2013-03-20 02:19:10

+0

當然,我將它設置爲True – Leonardo 2013-03-20 13:34:42

+0

因爲你的訪問後,在阿賈克斯 – catherine 2013-03-20 14:51:45

回答

1
$.ajax({ 
    url: "/forms/"+model+"/", 
    type: "POST", 
    cache: false, 
    data: { 'csrfmiddlewaretoken': '{{csrf_token}}' }, 
    success: 
     function(result){ 
      $("#myModalLabel").html(result); 
      $("#companyModal").modal("show"); 
      }, 
    error: 
     function(xhr){ 
      alert("Error: " + xhr.statusText); 
      //alert(xhr.responseText) --> to get the full details of error 
      return false; 
      } 
    }); 
1

\W(U ppercase)匹配任何 - 字母數字字符。您可能應該使用\w(小寫字母),它與任何字母數字字符匹配。

urls.py

url(r'^forms/(?P<model>[\w-]+)/$','.views.form_create'), 
+0

定義CSRF現在我看到一個內部服務器錯誤使用小寫字母w^ – Leonardo 2013-03-20 00:11:39

+0

這可能意味着現在它已經匹配了網址後,問題是其他地方。我不確定''.views.form_create''符號,這可能是罪魁禍首,否則在你的視圖功能的東西。答案實際上超出了你原來的問題範圍。您可以問一個新的問題,提供收到的內部服務器錯誤的詳細信息,以便於幫助! – 2013-03-20 00:19:32