我有一个getJSON()调用,用于访问返回某些数据的视图。该调用有效并且数据被检索但是尽管如此,我仍然会收到500错误。
Failed to load resource: the server responded with a status of 500 (INTERNAL SERVER ERROR)
MultiValueDictKeyError at /album_ajax/
"Key u'reid' not found in <QueryDict: {}>"
Request Method: GET
Request URL: http://127.0.0.1:8000/album_ajax/
Django Version: 1.4.3
Python Executable: /usr/bin/python
Python Version: 2.7.1
Python Path: ['/Users/Santi/programming/feastfm', '/Library/Python/2.7/site-packages/pip-1.2.1-py2.7.egg', '/Library/Python/2.7/site-packages/South-0.7.6-py2.7.egg', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', '/Library/Python/2.7/site-packages']
我看过SO并试图询问IRC,但一直无法弄清楚为什么我会收到此错误。 view / call的语法直接从:http://lethain.com/two-faced-django-part-5-jquery-ajax/
复制views.py
def get_album_tracks(request):
get = request.GET.copy()
reid = get["reid"]
url = "http://www.musicbrainz.org/ws/2/release/"+reid+"?fmt=json&inc=artist-credits+recordings"
data = urllib2.urlopen(url)
api_results = json.load(data)
tracks = []
for entry in api_results['media']:
for track in entry['tracks']:
artist = track['artist-credit'][0]['name']
title = track['title']
tracks.append({'artist':artist, 'title':title})
# results = {'reid':reid, 'tracks':tracks}
results = {'test':"is this working?", "reid":reid, 'tracks':tracks}
return_json = simplejson.dumps(results)
return HttpResponse(return_json, mimetype='application/json')
getJSON()调用
$.getJSON("/album_ajax/", {"reid": reid}, function(json){
if(json['tracks']){
//alert("json?: " + json["tracks"]+" reid: "+json['reid']);
// var album = $('data-reid ='+)
for(var i=0; i<json['tracks'].length; i++){
//console.log(json['tracks'][i]['title'])
var artist = json['tracks'][i]['artist']
var title = json['tracks'][i]['title']
$(ul).append("<li class = 'album-track track' data-artist = '"+escape(artist) +"' data-title ='" + escape(title) +"'>"+artist +" - "+ title+"</li>");
}
}
else{
alert('no results')
}
})
我不明白为什么我得到500错误并且运行成功功能。
修改
我在我的视图中放置了一个print语句,看起来视图函数被调用了两次,一次是空的QueryDict,一次是一个包含reid变量的QueryDict:
album_tracks was just called
<QueryDict: {}>
album_tracks was just called
<QueryDict: {u'reid': [u'0447570d-4804-49f1-9396-d71ddd8f59c4']}>
我仍然不确定代码在哪里进行两次调用。
编辑2
包括具有getJSON()调用的整个函数
$('.album').click(function(e){
var $target = $(e.target);
// if(!$target.is("ul"))
if(!$target.is("div")) //magic happens here!!
{
return;
}
if($(this).children("li").length >= 1){
$(this).children("li").toggle();
}
else{
var reid = $(this).data('reid');
ul = this;
var data = {"reid": reid};
// var args = { type:"GET", url:"/album/", data:data, complete:done };
// $.ajax(args);
$.getJSON("/album_ajax/", data, function(json){
if(json['tracks']){
//alert("json?: " + json["tracks"]+" reid: "+json['reid']);
// var album = $('data-reid ='+)
for(var i=0; i<json['tracks'].length; i++){
//console.log(json['tracks'][i]['title'])
var artist = json['tracks'][i]['artist']
var title = json['tracks'][i]['title']
$(ul).append("<li class = 'album-track track' data-artist = '"+escape(artist) +"' data-title ='" + escape(title) +"'>"+artist +" - "+ title+"</li>");
}
}
else{
alert('no results')
}
})
}
});
答案 0 :(得分:0)
事实证明我在课堂上有多个元素,其中一个元素具有使ajax调用工作所需的数据属性,另一个没有。他们都在开火,一人失败而另一人失败。
感谢@ jesse-vogt告诉我在html代码中查找.album类。