0

This is my application.py code

@app.route("/")
@login_required
def index():
    """Show portfolio of stocks"""

    stocks = db.execute("SELECT stock, number FROM portfolio WHERE id = :id", id = session['user_id'])

    result = db.execute("SELECT cash FROM users WHERE id = :id", id = session['user_id'])

    total_cash = float(result[0]['cash'])

    grand_total = total_cash

        for stock in stocks:
            symbol = str(stocks["stock"])
            shares = int(stocks["number"])
            quote = lookup(symbol)
            price = float(quote['price'])
            total_value_share = float(price * shares)
            grand_total += total_value_share

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

This is my html code

{% extends "layout.html" %}

{% block title %}
    index
{% endblock %}

{% block main %}
    <h3>Portfolio</h3>
    <tr>
        <th>Stocks Owned</th>
        <th>Shares Owned</th>
        <th>Current Price</th>
        <th>Total Value</th>
    </tr>
        {% for stock in stocks %}
        <tr>
            <td>{{ stock.symbol }}</td>
            <td>{{ stock.shares }}</td>
            <td>{{ stock.price }}</td>
            <td>{{ stock.total_value_share }}</td>
        </tr>
        {% endfor %}

    <h5>Current cash balance: {{ cash }}<br>
        Grand total value: {{ grand_total }}
    </h5>

{% endblock %}

TypeError: list indices must be integers or slices, not str

AttributeError: 'TypeError' object has no attribute 'name'

are my errors I think.

Can you help me out?

1 Answer 1

1

Each element in the list is called stock so you have to use stock['stock'] rather than stocks.

1
  • Oh yeah you are right! Thanks!
    – sksch1663
    Oct 2, 2018 at 5:24

You must log in to answer this question.

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