When there is no "q"
in query string, you will get None
.None
has no methods named split
, but string has.
message = request.args.get('q').split()
should be:
message = request.args.get('q', '').split()
Domanda
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)
In the code above, when I run this Flask app I get an error AttributeError: 'NoneType' object has no attribute 'split
from the home function. When I remove this, I also get an error on the ' '.join(message)
. Now when both of these are removed I refresh the page and the code runs, but not with the correct outputs. Next, I added the split and joins back in and refreshed the page and the code works perfectly, just as it should with no errors. How can I get this to run properly with out having to remove, refresh and add the join and split?
Soluzione
When there is no "q"
in query string, you will get None
.None
has no methods named split
, but string has.
message = request.args.get('q').split()
should be:
message = request.args.get('q', '').split()