2013-03-19 66 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