Error every time I make a Flask Requests

Asked

Viewed 119 times

0

When I make requests from an Api to show the number of players in my template where "For in range (200)" returns the data and not the error.

<table class="cor">
                <tr class="fundo">
                    <th>Tripulante</th>
                    <th>Voo</th>
                    <th>DEP - ARR</th>
                    <th>Altitude</th>
                    <th>Velocidade</th>
                    <th>Online</th>
                    <th>Progresso</th>
                </tr>
                {% for i in range(200) %}
                    {% if "IFBR" in data[i]['DisplayName'] %}
                        <tr>
                            <td>{{data[i]['DisplayName']}}</td>
                            <td>{{data[i]['CallSign']}}</td>
                            <td>-</td>
                            <td>{{"{0:.0f} ft".format (data[i]['Altitude'])}}</td>
                            <td>{{"{0:.0f} kts".format (data[i]['Speed'])}}</td>
                            <td>Expert Server</td>
                        </tr>
                    {% endif %}
                {% endfor %}
            </table>

inserir a descrição da imagem aqui

But when I put up 200 he’s wrong

jinja2.exceptions.UndefinedError
jinja2.exceptions.UndefinedError: list object has no element 246

Traceback (most recent call last)
File "/home/dalmo/Documentos/flask_IF/venv/lib/python3.5/site-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/home/dalmo/Documentos/flask_IF/venv/lib/python3.5/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/home/dalmo/Documentos/flask_IF/venv/lib/python3.5/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/dalmo/Documentos/flask_IF/venv/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/dalmo/Documentos/flask_IF/venv/lib/python3.5/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/home/dalmo/Documentos/flask_IF/venv/lib/python3.5/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/dalmo/Documentos/flask_IF/venv/lib/python3.5/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/dalmo/Documentos/flask_IF/venv/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/dalmo/Documentos/flask_IF/venv/lib/python3.5/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/home/dalmo/Documentos/flask_IF/venv/lib/python3.5/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/dalmo/Documentos/flask_IF/app/__init__.py", line 44, in expert
return render_template('index.html', data= serv_1())
File "/home/dalmo/Documentos/flask_IF/venv/lib/python3.5/site-packages/flask/templating.py", line 134, in render_template
context, ctx.app)
File "/home/dalmo/Documentos/flask_IF/venv/lib/python3.5/site-packages/flask/templating.py", line 116, in _render
rv = template.render(context)
File "/home/dalmo/Documentos/flask_IF/venv/lib/python3.5/site-packages/jinja2/environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "/home/dalmo/Documentos/flask_IF/venv/lib/python3.5/site-packages/jinja2/environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/dalmo/Documentos/flask_IF/venv/lib/python3.5/site-packages/jinja2/_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "/home/dalmo/Documentos/flask_IF/app/templates/index.html", line 30, in top-level template code
{% if "IFBR" in data[i]['DisplayName'] %}
File "/home/dalmo/Documentos/flask_IF/venv/lib/python3.5/site-packages/jinja2/environment.py", line 411, in getitem
return obj[argument]
jinja2.exceptions.UndefinedError: list object has no element 246
The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error.
To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.

You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:

dump() shows all variables in the frame
dump(obj) dumps all that's known about the object
Brought to you by DON'T PANIC, your friendly Werkzeug powered traceback interpreter.

inserir a descrição da imagem aqui

1 answer

1

jinja2.exceptions.Undefinederror: list Object has no element 246

I believe the function fails when you try to pass a number greater than 245. That’s because the list only has 245 elements.

You can work around this problem by iterating directly under the list, without a range:

<table class="cor">
    <tr class="fundo">
        <th>Tripulante</th>
        <th>Voo</th>
        <th>DEP - ARR</th>
        <th>Altitude</th>
        <th>Velocidade</th>
        <th>Online</th>
        <th>Progresso</th>
    </tr>
    {% for element in data %}
        {% if "IFBR" in element['DisplayName'] %}
            <tr>
                <td>{{element['DisplayName']}}</td>
                <td>{{element['CallSign']}}</td>
                <td>-</td>
                <td>{{"{0:.0f} ft".format (element['Altitude'])}}</td>
                <td>{{"{0:.0f} kts".format (element['Speed'])}}</td>
                <td>Expert Server</td>
            </tr>
        {% endif %}
    {% endfor %}
</table>

Browser other questions tagged

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