I finally made some forward progress by myself, and I have all the variable rows filled in, buy the stock and symbol show the same thing, when I would like one to say "AAPL" and the other to say "Apple" and I'm not sure what to change to get it to do this. Also, if there is anything else I need to add to index to make it better or correct, please tell me, I still have to implement sell. Code:

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:
    stock = purchase["stock"]
    shares = purchase['sumshares']
    purchase['symbol'] = purchase['stock']
    currentstock = lookup(purchase['stock'])
    purchase['price'] = currentstock['price']
    purchase['singlestocktotal'] = purchase['sumshares'] * purchase['price']
    purchase["totalvalstock"] = round(purchase['singlestocktotal'],2)
    totalvalstock += purchase['totalvalstock']

# calculate total of all stock holdings at their current price and whatever cash the user has left.
grandtotalstocks = round(totalvalstock,2)
overalltotal = round(grandtotalstocks + usercash,2)
# call render_template and pass variables to html template
return render_template("index.html", purchases=purchases, usercash=usercash, grandtotalstocks=grandtotalstocks,overalltotal=overalltotal,)

{% extends "layout.html" %}

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

{% block main %}
<table width = 100%>

    {% for purchase in purchases %}


    {% endfor %}
        <td>Total Value of Stocks</td>
        <td>Total Assets</td>
{% endblock %}

Also here's a screenshot for index

enter image description here

1 Answer 1


It looks like stock is the symbol for the stock in the database, and it is being assigned to both the name and symbol.

for purchase in purchases:
    stock = purchase["stock"]
    purchase['symbol'] = purchase['stock']

I don't see anywhere in the code where the actual stock name is retrieved from the database or anywhere else.

The choice of field names is ambiguous. By using "stock", it is intuitively ambiguous whether the field contains the stock's name or symbol. Renaming the vars and database field names to 'symbol' and 'stock_name' would be far more intuitive and clear about their contents and would eliminate any ambiguity of what the data represents.

Unlikely, but it would also be worthwhile to check that the stock symbol isn't being stored in the stock name field in the database.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance.

  • I see your point and I will change that- but I'm not sure what value purchase['stock'] should equal so that it won't give just the symbol. I want to get the actual name
    – Joel Banks
    Oct 2, 2018 at 23:23
  • There are many ways to handle this. You can look up the name based on the symbol. You can store both fields in the transactions table, create a table of symbols and stock names and select from there, etc. You could also keep a portfolio table with both fields and the current number of shares owned by each user. It's up to you. Just keep in mind that name and symbol are two separate fields.
    – Cliff B
    Oct 2, 2018 at 23:49
  • I just don't understand what needs to go into the brackets to do the full stock name, because purchase['name'] doesn't work and I'm not sure what needs to be in that place.
    – Joel Banks
    Oct 11, 2018 at 0:37
  • It may not be that simple. Did you create fields in the database table(s) to separately store the stock symbol and the stock name? That's where you need to query the two fields from. You may need to redo one of your database tables.
    – Cliff B
    Oct 11, 2018 at 10:17

You must log in to answer this question.

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