Search code examples
pythonflaskjinja2

How to show all results of a list in jinja?


this is my template:

{% for item in naa %} 
    <a href="post/{{item['id']}}/{{item['about']}}">{{item['about']}}</a>
{% endfor %}

and flask:

cur.execute("SELECT post_id FROM favorites WHERE username = %s",[session['username']])
data=cur.fetchall()
naa = []
for row in data:
    pos_id = row["post_id"]
    cur.execute("SELECT* FROM posts WHERE id=%s ORDER BY created_at DESC",[pos_id])
    naa.append(cur.fetchall())
cur.close()
return render_template("favoritesm.html",naa = naa)

it's showing results but broken links like:

localhost/post//

so what's the problem and how to fix it?

THANKS


Solution

  • .fetchall() returns a list of tuples. Therefore .append is creating a list of lists. I believe you want to use .extend instead.

    naa.extend(cur.fetchall())
    

    See the answer to What is the difference between Python's list methods append and extend?