Flask AttributeError: 'NoneType' object has no attribute 'split'

metersk
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?

iMom0

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()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

AttributeError: 'NoneType' object has no attribute 'split'

From Dev

Django: AttributeError: 'NoneType' object has no attribute 'split'

From Dev

Python Flask: AttributeError: 'NoneType' object has no attribute 'is_active'

From Dev

Flask error: AttributeError: 'NoneType' object has no attribute 'startswith'

From Dev

Python Flask: AttributeError: 'NoneType' object has no attribute 'count'

From Dev

Wsgiref Error: AttributeError: 'NoneType' object has no attribute 'split'

From Dev

Wsgiref Error: AttributeError: 'NoneType' object has no attribute 'split'

From Dev

'NoneType' object has no attribute 'split'

From Dev

AttributeError: object has no attribute 'split'

From Dev

AttributeError: object has no attribute 'split'

From Dev

AttributeError: 'NoneType' object has no attribute 'ravel'

From Java

AttributeError: 'NoneType' object has no attribute 'loader'

From Dev

AttributeError: 'Nonetype' object has no attribute '_info'

From Dev

AttributeError: 'NoneType' object has no attribute 'endswith'

From Java

AttributeError: 'NoneType' object has no attribute 'roles'

From Dev

Celery - AttributeError: 'NoneType' object has no attribute 'delay'

From Dev

AttributeError: 'NoneType' object has no attribute 'lower' python

From Dev

Python: AttributeError: 'NoneType' object has no attribute 'findNext'

From Dev

AttributeError: 'NoneType' object has no attribute 'replace'

From Dev

Python AttributeError: NoneType object has no attribute 'close'

From Dev

Python: AttributeError: 'NoneType' object has no attribute 'groups'

From Dev

Exception AttributeError: "'NoneType' object has no attribute 'path'" in

From Dev

AttributeError: 'NoneType' object has no attribute 'loader'

From Dev

Python - AttributeError: 'NoneType' object has no attribute 'cursor'

From Dev

AttributeError: 'NoneType' object has no attribute 'endswith'

From Dev

Python: AttributeError: 'NoneType' object has no attribute 'start'

From Dev

Getting AttributeError:'NoneType' object has no attribute getText

From Dev

AttributeError: 'NoneType' object has no attribute 'add'

From Dev

Python: AttributeError: 'NoneType' object has no attribute 'findNext'

Related Related

  1. 1

    AttributeError: 'NoneType' object has no attribute 'split'

  2. 2

    Django: AttributeError: 'NoneType' object has no attribute 'split'

  3. 3

    Python Flask: AttributeError: 'NoneType' object has no attribute 'is_active'

  4. 4

    Flask error: AttributeError: 'NoneType' object has no attribute 'startswith'

  5. 5

    Python Flask: AttributeError: 'NoneType' object has no attribute 'count'

  6. 6

    Wsgiref Error: AttributeError: 'NoneType' object has no attribute 'split'

  7. 7

    Wsgiref Error: AttributeError: 'NoneType' object has no attribute 'split'

  8. 8

    'NoneType' object has no attribute 'split'

  9. 9

    AttributeError: object has no attribute 'split'

  10. 10

    AttributeError: object has no attribute 'split'

  11. 11

    AttributeError: 'NoneType' object has no attribute 'ravel'

  12. 12

    AttributeError: 'NoneType' object has no attribute 'loader'

  13. 13

    AttributeError: 'Nonetype' object has no attribute '_info'

  14. 14

    AttributeError: 'NoneType' object has no attribute 'endswith'

  15. 15

    AttributeError: 'NoneType' object has no attribute 'roles'

  16. 16

    Celery - AttributeError: 'NoneType' object has no attribute 'delay'

  17. 17

    AttributeError: 'NoneType' object has no attribute 'lower' python

  18. 18

    Python: AttributeError: 'NoneType' object has no attribute 'findNext'

  19. 19

    AttributeError: 'NoneType' object has no attribute 'replace'

  20. 20

    Python AttributeError: NoneType object has no attribute 'close'

  21. 21

    Python: AttributeError: 'NoneType' object has no attribute 'groups'

  22. 22

    Exception AttributeError: "'NoneType' object has no attribute 'path'" in

  23. 23

    AttributeError: 'NoneType' object has no attribute 'loader'

  24. 24

    Python - AttributeError: 'NoneType' object has no attribute 'cursor'

  25. 25

    AttributeError: 'NoneType' object has no attribute 'endswith'

  26. 26

    Python: AttributeError: 'NoneType' object has no attribute 'start'

  27. 27

    Getting AttributeError:'NoneType' object has no attribute getText

  28. 28

    AttributeError: 'NoneType' object has no attribute 'add'

  29. 29

    Python: AttributeError: 'NoneType' object has no attribute 'findNext'

HotTag

Archive