Django:/ books / search /的ValueError需要多于1个值来解包

时间:2016-04-30 07:59:16

标签: python django

我是初学者,我无法理解为什么会出现此错误。从多个帖子中尝试了很多内容:Django forms: need more than 1 value to unpackValueError: need more than 1 value to unpackPython - ValueError: need more than 1 value to unpackValueError: need more than 1 value to unpack, django email error但结果为零。

file views.py

def query(self,q, type = None):
        #q="python"
        rows, wordids = self.getmatchrows(q) #q=python
        scores = self.getscoredlist(rows, wordids, type)
        rankedscores = sorted([(score, url) for (url, score) in scores.items()],reverse = 1)
        #print the first 20 results on the screen

        for (score, urlid) in rankedscores[0:20]:
            t= self.textcomplexity(urlid)
            print '%f\t%s' %(score, self.geturlname(urlid))
            return HttpResponse(t)      

def search_form(request):
    return render(request, 'books/index.html')

def search(request):
    global message
    if 'q' in request.GET:
        message = request.GET['q']
        searc=searcher("db.sqlite3")
        wordids,urls=searc.query(message,'qi') # error pointing here

    else:
        message = 'You submitted an empty form.'

错误:

ValueError at /books/search/ need more than 1 value to unpack at wordids,urls=searc.query(message,'qi')

任何想法?请帮忙

1 个答案:

答案 0 :(得分:0)

在您的代码片段中,查询方法返回HttpResponse(如果for循环执行)或None。它们都不是您在搜索视图中所期望的2个元素的元组。检查您的查询方法并返回正确的结果,很可能您需要我看到的元组(分数,网址)列表。