2017-01-10 44 views
1

我是django和ajax的新手,所以我一直在研究項目以瞭解它。我有兩個按鈕,一個添加一個標記,一個刪除標記。將AJAX數據發送到錯誤的Django視圖

這裏是views.py

@csrf_exempt 
def save(request): 
    searchVar = request.POST.getlist('search[]') 
    waypoint = Waypoint() 
    waypoint.name = searchVar[0] 
    waypoint.geometry = ('POINT(' + searchVar[2] + " " + searchVar[1] + ')') 
    waypoint.save() 
    return HttpResponse(json.dumps(dict(isOk=1)), content_type='application/json') 

@csrf_exempt 
def remove(request): 
    objectID = request.POST.get('id') 
    point = get_object_or_404(Point, pk = objectID) 
    point.delete() 

這裏是urls.py

from django.conf.urls import patterns, url, include 

urlpatterns = patterns('googlemaps.waypoints.views', 
    url(r'^$', 'index', name='waypoints-index'), 
    url(r'', 'save', name='waypoints-save'), 
    url(r'', 'remove', name='waypoints-remove'), 
) 

,這裏是從JS ajax的文件

$('#saveWaypoints').click(function() { 
    var searchList = [search.name, search.geometry.location.lat(), search.geometry.location.lng()] 
    $.ajax({ 
     url : "waypoints-save", 
     type : "POST", 
     data : { search : searchList } 
    }, function (data) { 
     if (data.isOk) { 
      $('#saveWaypoints'); 
     } else { 
      alert(data.message); 
     } 
    }); 
}); 
$('#removeWaypoints').click(function() { 
    console.log(markerID); 
    $.ajax({ 
     url : "waypoints-remove", 
     type : "POST", 
     data : { id : markerID } 
    }, function (data) { 
     if (data.isOk) { 
      $('#removeWaypoints'); 
     } else { 
      alert(data.message); 
     } 
    }); 
}); 

保存按鈕作品好,但是當我點擊刪除按鈕時,我的控制檯日誌中出現這個錯誤

POST http://127.0.0.1:8000/waypoints-remove 500 (Internal Server Error) 

IndexError at /waypoints-remove 
list index out of range 

Request Method: POST 
Request URL: http://127.0.0.1:8000/waypoints-remove 

這個錯誤在我的服務器CMD

Internal Server Error: /waypoints-remove 
Traceback (most recent call last): 
    File "C:\Users\rnvitter\virtualenv4\myvenv\lib\site-packages\django\core\handlers\base.py", line 149, in get_response 
    response = self.process_exception_by_middleware(e, request) 
    File "C:\Users\rnvitter\virtualenv4\myvenv\lib\site-packages\django\core\handlers\base.py", line 147, in get_response 
    response = wrapped_callback(request, *callback_args, **callback_kwargs) 
    File "C:\Users\rnvitter\virtualenv4\myvenv\lib\site-packages\django\views\decorators\csrf.py", line 58, in wrapped_view 
    return view_func(*args, **kwargs) 
    File "C:\Users\rnvitter\virtualenv4\googlemaps\googlemaps\waypoints\views.py", line 23, in save 
    waypoint.name = searchVar[0] 
IndexError: list index out of range 
2017-01-09 22:40:11,781 - ERROR - Internal Server Error: /waypoints-remove 
Traceback (most recent call last): 
    File "C:\Users\rnvitter\virtualenv4\myvenv\lib\site-packages\django\core\handlers\base.py", line 149, in get_response 
    response = self.process_exception_by_middleware(e, request) 
    File "C:\Users\rnvitter\virtualenv4\myvenv\lib\site-packages\django\core\handlers\base.py", line 147, in get_response 
    response = wrapped_callback(request, *callback_args, **callback_kwargs) 
    File "C:\Users\rnvitter\virtualenv4\myvenv\lib\site-packages\django\views\decorators\csrf.py", line 58, in wrapped_view 
    return view_func(*args, **kwargs) 
    File "C:\Users\rnvitter\virtualenv4\googlemaps\googlemaps\waypoints\views.py", line 23, in save 
    waypoint.name = searchVar[0] 
IndexError: list index out of range 

這使我相信,從刪除按鈕AJAX調用數據被髮送到我保存視圖,沒有人知道嗎?

+0

在js文件中,您定義的url不起作用。您必須在html頁面中定義ajax,並在ajax中使用URL的語法:url:「{%url」waypoints-save'%}「,自動創建URL和您的ajax將採用正確的方法並使用django最新的方式來定義urls.py文件中的url沒有模式https://docs.djangoproject.com/zh/1.10/topics/http/urls/#example –

回答

3

你有你的urls.py文件設置錯誤。第一個參數是一個匹配URL的正則表達式,因此^$將匹配根路徑。 name參數用於獲取特定視圖的URL,例如當您使用reverse功能時。

^指示行的開始,並且$指示行的結束。兩者之間沒有任何關係,它將匹配一條空行。

要匹配另一個URL,例如/waypoints-save/,您需要編寫另一個正則表達式:^/waypoints-save/$。這將匹配行的開頭,後面跟着字符/waypoints-save/,後面是行的結尾。爲了讓您的整個urls.py設置正確,它應該是這樣的:

from django.conf.urls import patterns, url, include 

urlpatterns = patterns('googlemaps.waypoints.views', 
    url(r'^$', 'index', name='waypoints-index'), 
    url(r'^/waypoints-save/$', 'save', name='waypoints-save'), 
    url(r'^/waypoints-remove/$', 'remove', name='waypoints-remove'), 
) 
+0

做了這個伎倆,我很困惑如何在django工作的網址,但這有助於很多謝謝你! – captnvitman

2

您已經定義了具有相同的正則表達式2個URL條目。

url(r'', 'save', name='waypoints-save'), 
url(r'', 'remove', name='waypoints-remove'), 

由於​​將匹配r''(有沒有在這一個正則表達式,所以它會匹配任何東西),第一場比賽的勝利,並執行save方法。因此,您預計運行的方法remove未運行。