0

So I've been looking at this all morning and can't figure what I'm doing wrong. All my variables are producing the correct values so it seems to be something to do with my For loop. In the html template I have tried "stock.symbol" and "stock["symbol"] but neither produce any result. Please see my code below

def index():
    """Show portfolio of stocks"""
    stocks = db.execute("SELECT * FROM portfolio WHERE id = :id", id=session["user_id"])
    balance = (db.execute("SELECT cash FROM users WHERE id = :id", id=session["user_id"]))
    cash = balance[0]["cash"]

    total = float(cash)

    for stock in stocks:
        quote = lookup(stock["stock"])
        symbol = quote["symbol"]
        name = quote["name"]
        price = quote["price"]
        shares = int(stock["quantity"])
        value = float(price * shares)
        total += value

        return render_template("index.html", stocks=stocks, symbol=symbol, name=name, shares=shares, price=usd(price), value=usd(value), cash=usd(cash), total=usd(total))

    return render_template("index.html")

And my index.html template:

{% extends "layout.html" %}


{% block main %}
    <h3>Portfolio</h3>

    <table class="table">
        <thead>
            <tr>
                <th>Symbol</th>
                <th>Name</th>
                <th>Shares</th>
                <th>Price</th>
                <th>Total</th>
            </tr>
        </thead>
        <tbody>
            {% for stock in stocks %}
            <tr>
                <td>{{symbol}}</td>
                <td>{{name}}</td>
                <td>{{shares}}</td>
                <td>{{price}}</td>
                <td>{{value}}</td>
            </tr>
            {% endfor %}
            <tr>
                <th>Cash</th>
                <td></td>
                <td></td>
                <td></td>
                <td>{{cash}}</td>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td>{{total}}</td>
            </tr>

        </tbody>
    </table>
{% endblock %}
0

I'm thinking that there's a flaw elsewhere in your code that's causing the same data to be inserted/updated in the database so that all rows that meet the select criteria are being changed to ALL the exact data.

Have you tried running the query in the DB interface itself to look at the raw data in the db? Is it returning multiple different rows or multiple rows that are all identical?

  • When I print the values of the variables it goes through the loop and prints the correct values. I also just printed "stocks" and get a list of dicts as expected. Strange one! – GFitzP Oct 7 '20 at 20:18
  • I'd seen it mentioned that you could create and empty list and populate it using the for loop but that seems a bit clunky even though I'm stuck haha! – GFitzP Oct 7 '20 at 20:30
  • But have you used the human/database sql interface, phpmyadmin, to run the query itself and check the sql data generated directly? Not through any html code, but direct access to the database? If the data in the database is bad, you need to find out why. If it's good, then you can move on to why you're not extracting it correctly. – Cliff B Oct 7 '20 at 20:43
  • I have a portfolio table holding the symbol and quantity held by each user and when I run 'SELECT * FROM portfolio WHERE id = :id', it gives me all of them yea. Baffled lol, could it be to do with indentation of the return statement? When I move it back I get a different stock but multiple identical entries again – GFitzP Oct 7 '20 at 20:50
0

The following code is wrongly indented by a block.

return render_template("index.html", stocks=stocks, symbol=symbol, name=name, shares=shares, price=usd(price), value=usd(value), cash=usd(cash), total=usd(total))

At this level, for stock in stocks: is dominant. Thus what is being sent is stock and not stocks. Try to unindent and see/print out the output. That will give you a better idea of how to proceed. You might need another method means to store the output from your for loop as an intermediate step.

You must log in to answer this question.

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