0

When I try to use index, the table doesn't show all the values:

[![erroneous table][1]][1]

I'm not sure why it only shows some. My index code is as follows:

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

# Once a user makes a purchase they will be redirected to their index page
# Edit that to reflect the user's portfolio

# Get symbol, shares, cash from databases
purchases = db.execute("SELECT stock, SUM(shares) as sumshares FROM transactions WHERE user_id = :user_id GROUP BY stock ORDER BY stock ASC", user_id = session["user_id"])
usercash = db.execute("SELECT cash FROM users WHERE id = :id", id = session["user_id"])
usercash = usercash[0]["cash"]

# Declare variable for running total of stock value
totalvalstock = 0

# Purchases query will return dicts, loop to iterate over list of dicts.
for purchase in purchases:
    symbol = purchase["stock"]
    shares = purchase['sumshares']
    currentstock = lookup(purchase['stock'])
    purchase['price'] = currentstock['price']
    purchase['valbystock'] = purchase['sumshares'] * purchase['price']
    totalvalstock += purchase['valbystock']

# calculate total of all stock holdings at their current price and whatever cash the user has left.
grandtotal = totalvalstock + usercash

# call render_template and pass variables to html template
return render_template("index.html", stocks=purchases, usercash=usercash, grandtotal=grandtotal)

Here is index.html:

{% extends "layout.html" %}

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

{% block main %}
    <table width = 100%>
        <tr>
            <th>Symbol</th>
            <th>Name</th>
            <th>Shares</th>
            <th>Price</th>
            <th>Value</th>

        </tr>
        {% for stock in stocks %}

            <tr>
                <td>{{stock.symbol}}</td>
                <td>{{stock.name}}</td>
                <td>{{stock.price}}</td>
            </tr>

        {% endfor %}
        <tr>
            <td>Total Value</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>{{grandtotal}}</td>
        </tr>
        <tr>
            <td>Cash</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>{{usercash}}</td>
        </tr>
        <tr>
            <td>Total Assets</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>{{grandtotal}}</td>
        </tr>
    </table>
{% endblock %}
3
  • I guess the reason might be in index.html. BTW, look into HAVING clause if you want to exclude shares with sumshares being 0. It's like a WHERE applied after aggregation. – Blauelf Aug 8 '18 at 15:50
  • @Blauelf I will update my post with index.html – Joel Banks Aug 8 '18 at 16:11
  • Note that you have an erroneous <tr></tr> towards the end (above the “Total Assets” row. I doubt this is causing issues, but it may be best to fix anyway. – Tim Aug 9 '18 at 1:22
1

The variables/members/attributes referenced in index.html don't match the ones in the list you pass, there is no symbol, but stock. grandTotal is not same as grandtotal. You don't seem to use valbystock.

6
  • Ive edited my code to show what I have now, any idea why some of the numbers still dont show up? – Joel Banks Aug 11 '18 at 1:21
  • @JoelBanks you should ask a new question if you have a new issue. – Tim Aug 11 '18 at 2:43
  • @JoelBanks Read my answer again. purchase["stock"] and stock.symbol - "stock" is not same as "symbol" (you using same word for variable and its member is pretty confusing, you could have used symbol as the table field and member name to be more consistent). Also, you seem to write only three values per line, what do you expect? – Blauelf Aug 11 '18 at 11:28
  • @blauelf I changed "for stock in stocks" to "for purchase in purchases" and it aligns everything better, I don't understand what the "stock.symbol, stock.name, and stock.price" need to change to and I tried many names there – Joel Banks Aug 18 '18 at 1:00
  • The member/attribute names just have to match, so the template can find it. Easiest done if it's "symbol" in the form, in the database, in the script, in the template. – Blauelf Aug 18 '18 at 15:30

You must log in to answer this question.

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