python appen()之后结果为空

代码与原因如下:

jsonArrays=Arrays.append(ret)修改了Arrays的值,但是并没有返回值,jsonArrays的值为空,所以我们直接返回Arrays即可

#错误代码:
def view(request):
    a = request.GET['a']
    b = request.GET['b']
    input=str(a)
    algo=str(b)
    name,desc= recommend.recommend([input], algo)
    Arrays=[]
    for i in range(20):
        ret=[name[i],desc[i]]
        jsonArrays=Arrays.append(ret)
        rets={"Result":jsonArrays}
        retsj = json.dumps(rets).encode("gb2312")
    return HttpResponse(retsj)

#改为:
def view(request):
    a = request.GET['a']
    b = request.GET['b']
    input=str(a)
    algo=str(b)
    name,desc= recommend.recommend([input], algo)
    Arrays=[]
    for i in range(20):
        ret=[name[i],desc[i]]
        Arrays.append(ret)
        rets={"Result":Arrays}
        retsj = json.dumps(rets).encode("gb2312")
    return HttpResponse(retsj)

你可能感兴趣的:(python appen()之后结果为空)