0

The table is not able to access different variables to display in the table. Here is the index function

def index():
"""Show portfolio of stocks"""

"""Selecting shares that have been purchased already"""
stocks = db.execute("SELECT symbol, shares FROM portfolio WHERE userid = :userid", userid = session["user_id"])
if stocks:

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

"""to retrieve the current amount of cash the user owns"""
cashs = db.execute("SELECT * FROM users WHERE id=:ida", ida=session["user_id"])
if not cashs:
    return apology("cant retrieve cash", 400)

casha = float(cashs[0]["cash"])
if not casha:
    return apology("something with retrieving cash")

return render_template("index.html", stocks = stocks,  cash = casha)

here is the index.html function

{% extends "layout.html" %}

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

{% block main %}
    <table>
        <tr>
            <th> Symbol</th>
            <th> Name</th>
            <th> Shares</th>
            <th> Price</th>
            <th> Total</th>
        </tr>
        {% for stock in stocks %}
        <tr>
            <td>{{stock.symbol}}</td>
            <td>{{stock.name}}</td>
            <td>{{stock.quantity}}</td>
            <td>{{stock.price}}</td>
            <td>{{stock.value}}</td>
        </tr>
        {% endfor %}
        <tr>
            <td> cash </td>
            <td></td>
            <td></td>
            <td></td>
            <td>{{ cash }}</td>
        </tr>

    </table>
{% endblock %}

sdited code :-

 rows = db.execute("SELECT symbol, shares FROM portfolio WHERE userid = :userid", userid = session["user_id"])
if rows:
    for stock in rows:
        stock["symbol"] = rows["symbol"]
        stock["quantity"] = rows["shares"]
        quote = lookup(symbol)
        stock["price"] = quote["price"]
        stock["name"] = quote["name"]
        stock["value"] = quantity * price

"""to retrieve the current amount of cash the user owns"""
cashs = db.execute("SELECT * FROM users WHERE id=:ida", ida=session["user_id"])
if not cashs:
    return apology("cant retrieve cash", 400)

casha = float(cashs[0]["cash"])
if not casha:
    return apology("something with retrieving cash")

return render_template("index.html", stocks = rows,  cash = casha)
0

The stocks array is being populated with symbol and shares keys from the database. That is all that is being sent to index.html here return render_template("index.html", stocks = stocks, cash = casha).

The for stock in stocks: loop is not creating new elements in stocks.
EDIT --> Why is that? The loop creates variables for each of the elements needed in index.html but never stores them in stock (and by extension, never stored in stocks). You need to create new key/value pairs in a stock by assigning them. For instance stock["value"] = value.

You have the idea in the edited code. However, it will only work if rows is the empty set (ie the SELECT does not return any rows). Why is that? Because this stock["symbol"] = rows["symbol"] will give a TypeError if it tries to process a stock. There is no rows["symbol"] because rows is a list of dictionaries indexed to 0. And fixing that will result in cascading errors, so (free) advice, go back to the original version and populate stocks properly.

All things being equal, I cannot find a condition where the cash line does not display the cash value.

  • Please check the edited code. The code is still not displaying the table and i am having problems understanding the dictionary and libraries involved in retrieving stock. Moreover it is not even displaying cash which is separate from that block – Kushagra Chawda Sep 3 '18 at 10:14
  • When you say "not displaying the table" do you mean it is giving an Internal Server Error? – DinoCoderSaurus Sep 3 '18 at 11:35
  • No, it is not displaying the values. There are just the headings and the word cash in the next row. – Kushagra Chawda Sep 3 '18 at 12:19
  • I have added detail to the original answer. – DinoCoderSaurus Sep 4 '18 at 14:26
  • still struggling – Kushagra Chawda Sep 6 '18 at 11:45

You must log in to answer this question.

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