0

Quando faço um requests de uma Api para mostra quantidade de jogadores no meu template onde "For in range (200)" ele me retorna os dados e não da erro.

<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

Mas quando Coloco acima de 200 ele da erro

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 Resposta 1

1

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

Acredito que a função falhe quando você tenta passar um número maior do que 245. Isso porque a lista só tem 245 elementos.

Você pode contornar esse problema iterando diretamente sob a lista, sem um 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>

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .