【问题标题】:How to show all results of a list in jinja?如何在 jinja 中显示列表的所有结果?
【发布时间】:2019-06-15 21:47:52
【问题描述】:

这是我的模板:

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

和烧瓶:

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)

它正在显示结果,但链接断开,例如:

localhost/post//

那么问题是什么以及如何解决?

谢谢

【问题讨论】:

    标签: python flask jinja2


    【解决方案1】:

    .fetchall() 返回一个list of tuples。因此 .append 正在创建一个列表列表。我相信您想改用 .extend。

    naa.extend(cur.fetchall())
    

    请参阅the answerWhat is the difference between Python's list methods append and extend?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多