0

I have the following row within a table:

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

when the first instance of the {% for z in recentResultHistory %} {{ z.0 }} {% endfor %} runs I get the expected result. When it runs for the second time it produces no results like it is not looping at all. Do I need to reset the loop in some way?

The variable is created in my django view.py as follows:

        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
5
  • Is recentResultHistory a generator? Commented Oct 16, 2016 at 11:16
  • We can't possibly answer this without seeing the source of recentResultHistory. And is this Django, or Jinja, or what? Commented Oct 16, 2016 at 11:17
  • My apologies, question updated to include the source. It is django based. Commented Oct 16, 2016 at 11:20
  • You should call fetchall and then recentResultHistory should be a list and you have no problems. Commented Oct 16, 2016 at 11:21
  • Sorry Daniel, i am using "recentResultHistory = cursor.fetchall". Is this what you meant? Commented Oct 16, 2016 at 11:25

1 Answer 1

0

I should be using list(cursor) rather than cursor.fetchall(). Daniel and dkasak gave me the inspiration to look and I found the following which solved my issue:

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.