0

I've spent a lot of time trying to solve this problem and can't seem to find were I went wrong. Any help is greatly appreciated.

I am unable to render the user's cash balance on the index.

def index():
    #see notes
    """
    display an HTML table summarizing, for the user currently logged in the following""" 
    #which stocks the user owns,



user_portfolio_rows = db.execute("SELECT * FROM portfolio WHERE user_id = :id", id=session["user_id"])
#the numbers of shares owned,
#if user_portfolio_rows == None:
    #return apology("Your portfolio is empty")
i = 0

"""For every row in the user's portfolio, check the current price of the share 
and calculate the total value for each symbol the user owns"""
for row in user_portfolio_rows:

    #get the symbol 
    symbol = user_portfolio_rows[i]["symbol"]
    #look up the current price of the symbol
    looked_up_symbol = lookup(symbol)
    current_price = looked_up_symbol['price']

    #shares owned
    shares = user_portfolio_rows[i]["shares"]
    #calculate the total value
    total_value = current_price * shares

    #insert the current price and total value into the database
    #db.execute("INSERT INTO portfolio (current_price, total_value) VALUES (:current_price, :total_value) WHERE ", user_id=session["user_id"], symbol=symbol, stock_price=stock_price, no_of_shares=no_of_shares, total_cost=total_cost)
    db.execute("UPDATE portfolio SET current_price = :current_price, total_value = :total_value WHERE user_id = :user_id AND symbol = :symbol", current_price=current_price, total_value=total_value, user_id=session["user_id"], symbol=symbol)

    i = i + 1 



#Also display the user’s current cash balance along with 

cash_balance = db.execute("SELECT * FROM users WHERE id = :id", id=session["user_id"])
#a grand total (i.e., stocks' total value plus cash).
return render_template("index.html", portfolio=user_portfolio_rows, cash=cash_balance)

Here is my index page html

{% extends "layout.html" %}


{% block title %}
    Index
{% endblock %}

{% block main %}
<!--HTML table with user's portfolio-->



    <div class="table-title">
        <h3></h3>
    </div>


    <table style="width:100%">
        <tr>
            <th>Symbol</th>
            <th>Shares</th>
            <th>Stock Price</th>
            <th>Total Value</th>
        </tr>
        {% for data in portfolio %}


        <tr>
            <td>{{ data.symbol }}</td>
            <td>{{ data.shares }}</td>
            <td>{{ data.current_price }}</td>
            <td>{{ data.total_value }}</td>

        </tr>

        {% endfor %}
        <tr>
            <td colspan="3">Cash Balance</td>
            <td colspan="1">{{ cash.cash }}</td>


        </tr>
        <tr>
            <td colspan="3">Grand Total</td>
            <td colspan="1">Total amount here in bold</td>
        </tr>


    </table>






{% endblock %}

My database

enter image description here

This is what I keep getting

enter image description here

0

cash_balance is a list because:

If str is a SELECT, then execute returns a list of zero or more dict objects, inside of which are keys and values representing a table’s fields and cells, respectively.

There is no cash.cash in index.html. There are any number of ways to fix this, I'll let you work it out.

1
  • Thank you for the hint. I still had to struggle to fully grasp what you meant. (newbie blues) Thanks as always for you help. It is greatly appreciated. – Salamit Jul 3 '17 at 12:19
0

I will just add to great hint that DinoCoderSaurus said above because as a beginner, I still had to struggle to understand what he said.

Essentially, in my index function, the following line

cash_balance = db.execute("SELECT * FROM users WHERE id = :id", id=session["user_id"])

returned a list of dict objects that looked something like this:

{'cash': 8568.569999999998, 'username': 'sally12', 'id': 11, 'hash': '$6$rounds=656000$UzOS.hzWEW6hT4Sp$OlrYf1Wqn2JyXbwFSdiA.IZwlcSP2.KRvf0Gc3OE44ngMgBfRfxlx/ljGYTLPz9v6Wg6Xb.Eq/lOaWLhSmshU1'}]

To access the value of cash, checkout 'rows' in the login function.

# query database for username
        rows = db.execute("SELECT * FROM users WHERE username = :username", username=request.form.get("username"))
        #cash_balance = db.execute("SELECT * FROM users WHERE id = :id", id=session["user_id"])

        # ensure username exists and password is correct
        if len(rows) != 1 or not pwd_context.verify(request.form.get("password"), rows[0]["hash"]):
            return apology("invalids username and/or password")

        # remember which user has logged in
        session["user_id"] = rows[0]["id"]

You need the index (which in rows is [0] plus the name of the column (the key) which in rows is ["id"] to get the value which in rows is the user_id.

Hopefully, this helps anyone else with a similar problem.

You must log in to answer this question.

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