I have the below code for my application:

    #valid stock row queries
valid_stocks_rows = db.execute("SELECT symbol, SUM(shares) as sum_total FROM ledger WHERE user_id=:user_id GROUP BY symbol HAVING sum_total > 0", user_id=session["user_id"])

for row in valid_stocks_rows:
    stock_symbol = row["symbol"]
    stock_shares = row["sum_total"]
    quote = lookup(stock_symbol)
    share_price = quote["price"]

return render_template("portfolio.html", user_cash=user_cash, valid_stocks_rows=valid_stocks_rows)

And for my HTML, I also have the below code:


{% for row in valid_stocks_rows %}
    <td>{{ row.stock_symbol }}</td>
    <td>{{ row.stock_shares }}</td>
    <td>{{ row.share_price }}</td>
{% endfor %}

Why don't the variables pull over? What am I missing?

  • Try print out the valid_stocks_rows you got from the query. You will see it is a list of dictionaries. You are not storing the output of for row in valid_stocks_rows:; so nothing is been sent to your HTML file.
    – EddyBeor
    Dec 9 '20 at 20:24

The "loop" variables" are not being sent to the HTML.

What is the contents of valid_stocks_rows? It is the list of dicts returned from the execute. All the work done in the for loop is wasted; it sets variables to values and never does anything with them.

Perhaps add new key/value pairs to valid_stocks_rows in the for loop.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .