Python Flask Typeerror: 'Nonetype' argument is not iterable

Asked

Viewed 53 times

-3

I have a Web Application that uses Flask that was working right only it is now returning me an error.

The application does a search in the name of the players in a json file, in the execution of the for that is in index.html I am getting the following error:

Typeerror: 'Nonetype' argument is not iterable

main py.

from flask import Flask, render_template
import requests
import json


app = Flask(__name__)

def get_json(url):
    return json.loads(requests.get(url).text)


flight = get_json('http://infinite-flight-public-api.cloudapp.net/v1/Flights.aspx?'
            'apikey=78879b1d-3ba3-47de-8e50-162f35dc6e04&sessionid=7e5dcd44-1fb5-49cc-bc2c-a9aab1f6a856')

@app.route('/')
def index():    
    return render_template('index.html', data=flight)

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

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <table>
        <thead>
            <tr>
                <th>Nome</th>
            </tr>
        </thead>
        <tbody>
            {% for data in data %}
                {% if 'IFAB' in data['DisplayName'] %}
                <td>{{data['DisplayName']}}</td>
                {% endif %}
            {% endfor %}
        </tbody>
    </table>
</body>
</html>

error

builtins.TypeError
TypeError: o argumento do tipo 'NoneType' não é iterável

Traceback (última chamada mais recente)
Arquivo "C: \ Usuários \ Positivo \ AppData \ Local \ Programas \ Python \ Python37 \ lib \ site-packages \ flask \ app.py" , linha 2309 , em__call__
return self.wsgi_app (amb, start_response)
Arquivo "C: \ Usuários \ Positivo \ AppData \ Local \ Programas \ Python \ Python37 \ lib \ site-packages \ flask \ app.py" , linha 2295 , emwsgi_app
resposta = self.handle_exception (e)
Arquivo "C: \ Usuários \ Positivo \ AppData \ Local \ Programas \ Python \ Python37 \ lib \ site-packages \ flask \ app.py" , linha 1741 , emhandle_exception
reraise (exc_type, exc_value, tb)
Arquivo "C: \ Usuários \ Positivo \ AppData \ Local \ Programas \ Python \ Python37 \ lib \ site-packages \ flask \ _compat.py" , linha 35 , emreraise
aumentar o valor
Arquivo "C: \ Usuários \ Positivo \ AppData \ Local \ Programas \ Python \ Python37 \ lib \ site-packages \ flask \ app.py" , linha 2292 , emwsgi_app
resposta = self.full_dispatch_request ()
Arquivo "C: \ Usuários \ Positivo \ AppData \ Local \ Programas \ Python \ Python37 \ lib \ site-packages \ flask \ app.py" , linha 1815 , emfull_dispatch_request
rv = self.handle_user_exception (e)
Arquivo "C: \ Usuários \ Positivo \ AppData \ Local \ Programas \ Python \ Python37 \ lib \ site-packages \ flask \ app.py" , linha 1718 , emhandle_user_exception
reraise (exc_type, exc_value, tb)
Arquivo "C: \ Usuários \ Positivo \ AppData \ Local \ Programas \ Python \ Python37 \ lib \ site-packages \ flask \ _compat.py" , linha 35 , emreraise
aumentar o valor
Arquivo "C: \ Usuários \ Positivo \ AppData \ Local \ Programas \ Python \ Python37 \ lib \ site-packages \ flask \ app.py" , linha 1813 , emfull_dispatch_request
rv = self.dispatch_request ()
Arquivo "C: \ Usuários \ Positivo \ AppData \ Local \ Programas \ Python \ Python37 \ lib \ site-packages \ flask \ app.py" , linha 1799 , emdispatch_request
return self.view_functions [rule.endpoint] (** req.view_args)
Arquivo "c: \ Usuários \ Positivo \ Desktop \ Massa Nova (2) \ main.py" , linha 17 , emindex
return render_template ('index.html', data = flight)
Arquivo "C: \ Usuários \ Positivo \ AppData \ Local \ Programas \ Python \ Python37 \ lib \ site-packages \ flask \ templating.py" , linha 135 , emrender_template
contexto, ctx.app)
Arquivo "C: \ Usuários \ Positivo \ AppData \ Local \ Programas \ Python \ Python37 \ lib \ site-packages \ flask \ templating.py" , linha 117 , em_render
rv = template.render (contexto)
Arquivo "C: \ Usuários \ Positivo \ AppData \ Local \ Programas \ Python \ Python37 \ lib \ site-packages \ jinja2 \ asyncsupport.py" , linha 76 , emrender
retornar original_render (self, * args, ** kwargs)
Arquivo "C: \ Usuários \ Positivo \ AppData \ Local \ Programas \ Python \ Python37 \ lib \ site-packages \ jinja2 \ environment.py" , linha 1008 , emrender
return self.environment.handle_exception (exc_info, True)
Arquivo "C: \ Usuários \ Positivo \ AppData \ Local \ Programas \ Python \ Python37 \ lib \ site-packages \ jinja2 \ environment.py" , linha 780 , emhandle_exception
reraise (exc_type, exc_value, tb)
Arquivo "C: \ Usuários \ Positivo \ AppData \ Local \ Programas \ Python \ Python37 \ lib \ site-packages \ jinja2 \ _compat.py" , linha 37 , emreraise
raise value.with_traceback (tb)
Arquivo "c: \ Usuários \ Positivo \ Desktop \ Pasta Nova (2) \ templates \ index.html" , linha 17 , emtop-level template code
{% if 'IFAB' nos dados ['DisplayName']%}
TypeError: o argumento do tipo 'NoneType' não é iterável
  • Important you [Dit] your question and explain objectively and punctually the difficulty found, accompanied by a [mcve] of the problem and attempt to solve. To better enjoy the site, understand and avoid closures and negativities worth reading the Stack Overflow Survival Guide in English.

1 answer

-1


This is happening because the code is searching for null values. You can try to delete these null values or remove them from the search. Ex.:

   <tbody>
        {% for data in data %}
            {% if data['DisplayName'] is not None %}
                {% if 'IFAB' in data['DisplayName'] %}
                   <td>{{data['DisplayName']}}</td>
                {% endif %}
            {% endif %}
        {% endfor %}
   </tbody>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.