2014-01-12 102 views
2
from flask import Flask, render_template, request 
from sys import argv 
import requests 
import json 

app = Flask(__name__) 

def decrementList(words): 
    for w in [words] + [words[:-x] for x in range(1,len(words))]: 
     url = 'http://ws.spotify.com/search/1/track.json?q=' 
     request = requests.get(url + "%20".join(w)) 

     json_dict = json.loads(request.content) 
     track_title = ' '.join(w) 

     for track in json_dict["tracks"]: 
      if track["name"].lower() == track_title.lower() and track['href']: 
       return "http://open.spotify.com/track/" + track["href"][14:], words[len(w):] 

    return "Sorry, no more track matches found!" 

@app.route('/') 
def home(): 
    message = request.args.get('q').split() 
    first_arg = ' '.join(message) 

    results = [] 
    while message: 
     href, new_list = decrementList(message) 
     message = new_list 
     results.append(href) 

    return render_template('home.html', first_arg=first_arg, results=results) 

if __name__ == '__main__': 
    app.run(debug=True) 

在上面的代码,当我运行这个程序瓶从我家里功能得到一个错误AttributeError: 'NoneType' object has no attribute 'split。当我删除它时,我也在' '.join(message)上发生错误。现在,当这两个都被删除我刷新页面和代码运行,但没有正确的输出。接下来,我添加了拆分并重新加入,并刷新了页面,代码完美无缺,就像没有错误一样。我怎样才能得到这个正常运行,无需删除,刷新和添加连接和拆分?瓶AttributeError的:“NoneType”对象有没有属性“分裂”

回答

3

查询字符串中没有"q"时,将得到NoneNone没有名为split的方法,但字符串有。

message = request.args.get('q').split() 

应该是:

message = request.args.get('q', '').split() 
+0

啊好感谢 – metersk

相关问题