【问题标题】:Python Loop In HTMLHTML中的Python循环
【发布时间】:2017-02-25 10:42:53
【问题描述】:

我在一个表中有以下行:

<TR>
    <TD style="text-align:center;width:50px">{% for z in recentResultHistory %} {{ z.0 }} {% endfor %}</TD>
    <TD style="text-align:center;width:100px"></TD>
    <TD style="text-align:center;width:50px">V</TD>
    <TD style="text-align:center;width:100px"></TD>
    <TD style="text-align:center;width:50px">{% for z in recentResultHistory %} {{ z.0 }} {% endfor %}</TD>
</TR>

{% for z in recentResultHistory %} {{ z.0 }} {% endfor %} 的第一个实例运行时,我得到了预期的结果。当它第二次运行时,它不会产生任何结果,就像它根本没有循环一样。我需要以某种方式重置循环吗?

变量在我的django view.py中创建如下:

        cursor = connection.cursor()

        cursor.execute("""
                        select
                        team,
                        group_concat( concat(result, '-', convert(opponent USING utf8), '-', team_score, '-', opponent_score, '-', mstatus)
                                     order by fixturedate desc
                                    separator '|') as output from plain_result
                        where (select count(*)
                               from plain_result as p
                               where plain_result.team = p.team
                               and p.fixturedate>=plain_result.fixturedate) <= 5
                        group by team
                       """)

        recentResultHistory = cursor.fetchall

【问题讨论】:

  • recentResultHistory 是生成器吗?
  • 如果没有看到recentResultHistory 的来源,我们不可能回答这个问题。这是 Django,还是 Jinja,还是什么?
  • 抱歉,问题已更新以包含来源。它是基于 django 的。
  • 你应该调用fetchall然后recentResultHistory应该是一个列表,你没有问题。
  • 对不起,丹尼尔,我正在使用“recentResultHistory = cursor.fetchall”。这是你的意思吗?

标签: python html for-loop


【解决方案1】:

我应该使用 list(cursor) 而不是 cursor.fetchall()。 Daniel 和 dkasak 给了我寻找的灵感,我发现以下解决了我的问题:

cursor.fetchall() vs list(cursor) in Python

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    • 2016-12-31
    • 1970-01-01
    • 2010-10-19
    相关资源
    最近更新 更多