我一直在与django合作制作一个互动网页。但是当我尝试向django视图发出Http请求时。它抛出了这个错误,即使我有完全相同的代码,并且工作得很好。这是我的Python代码:
def test(request):
default = {"Error" : "Could not make request"}
name = request.GET.get('showname')
token = getToken()
showJs = searchShowId(name, token)
if showJs.get('Error'):
try:
token = refreshToken(token)
showJs = searchShowId(name, token)
return JsonResponse(showJs)
except KeyError as error:
token = login()
showJs = searchShowId(name, token)
return JsonResponse(showJs)
else:
return JsonResponse(showJs)
def image(request):
default = {"Error" : "Could not make request"}
id = request.GET.get('id')
return JsonResponse(image(id))
这是完整的错误:
Traceback:
File "C:\Users\\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\exception.py" in inner
35. response = get_response(request)
File "C:\Users\\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "C:\Users\\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\handlers\base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "E:\Documentos\Programming\DjangoProjects\mediaD\mediaDe\views.py" in image
33. return JsonResponse(image(id))
File "E:\Documentos\Programming\DjangoProjects\mediaD\mediaDe\views.py" in image
32. id = request.GET.get('id')
Exception Type: AttributeError at /image/
Exception Value: 'str' object has no attribute 'GET'
Javascript代码:
function search() {
console.log('Function Called')
var showName = document.getElementById('showName');
console.log(showName.value);
var name = stringToPost(showName.value);
$.ajax({
type: "GET",
url: "/request/",
data: {
showname: showName.value
},
success: function (dato) {
dato = dato['data'];
for (var i = 0; i < dato.length; i++) {
$.ajax({
type: "GET",
url: "/image/",
data: {
id : dato[i]['id']
},
success: function (datax) {
console.log(datax);
},
});
}
},
});
就像我已经说过的那样,测试功能完美无缺,但是图像并没有。它可能是什么?
答案 0 :(得分:1)
这是因为在您的image
函数中,您return image(id)
调用相同的函数,但这次使用的是id
而不是request
个对象。< / p>
尝试以不同的方式命名两件事,例如:您可以将视图功能重命名为image_view
。