for in range on the flask template

0

I'm in doubt, the API I'm getting returns me two data types "ID" and "Title" are various id and title and wanted to throw this in my table in a "for" template. I wanted to know if what I did is right because it does not return anything.

code in flask

from flask import Flask, render_template
import requests
import json

app = Flask(__name__)

def game():
    url = requests.get("https://jsonplaceholder.typicode.com/posts")
    resp = json.loads(url.text)
    return resp

@app.route("/")

def index():
    return render_template("index.html", data = game())



if __name__ == "__main__":

    app.run()

It's the template:

  <!doctype html>
<html>
<head>
    <title>Minha pagina</title>
    <meta charset="utf-8" />
</head>
<body>
    <h1>Teste de API</h1>
    <br>

    <table border = "1">
        <tr>
            <td align= middle width=100 bgcolor= #DAA520> ID </td>
            <td align= middle width=100 bgcolor= #DAA520> TITLE</td>
        </tr>
        <tr>
        {% for i in range(3) %}
            <td align= middle width=150>{{resp[i] ["id"]}}</td>
            <td align= middle width=150>{{resp[i]["title"]}}</td>
        {% endfor %}

        </tr>

    </table>

</body>


</html>
    
asked by anonymous 10.04.2018 / 17:25

1 answer

0

resp is not defined, it is data

{% for i in range(3) %}
    <td align= middle width=150>{{data[i] ["id"]}}</td>
    <td align= middle width=150>{{data[i]["title"]}}</td>
{% endfor %}
    
10.04.2018 / 17:58