我是django和ajax的新手,所以我一直在研究一个项目来学习它。我有两个按钮,一个添加标记,另一个删除标记。
这是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 :(得分:3)
您的urls.py
文件设置错误。第一个参数是匹配URL的正则表达式,因此^$
将匹配根路径。 name参数用于获取特定视图的URL,例如当您使用reverse
函数时。
^
表示一行的开头,$
表示一行的结尾。在它们之间没有任何东西,它将匹配一个空行。
要匹配其他网址,例如/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'),
)
答案 1 :(得分:2)
您已定义了两个具有相同正则表达式的URL条目。
url(r'', 'save', name='waypoints-save'),
url(r'', 'remove', name='waypoints-remove'),
由于/waypoints-remove
将匹配r''
(正则表达式中没有任何内容,因此它将匹配任何内容),第一个匹配获胜,并执行save
方法。因此,您预期运行的方法remove
未被运行。