我是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調用數據被髮送到我保存視圖,沒有人知道嗎?
在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 –