I have been trying to implementing my code for Index and index.html. My code for Index seems to work very well, but my index.html isn't. My implementation of index() and index.html are pasted below. For some reasons, the table is printed perfectly, as well as the table headers and the user's current cash balance and total asset, with the exception that all rows inside the for loop simply do not show up. Any advice why? I posted a similar question yesterday night, available here: CS50 Pset7: rows inside the for loop aren't showing up in index.html Thank you very much and I appreciate any help!!!


def index():
    """User's cover page."""

    # Pull past transactions from database
        stocks = db.execute("SELECT stock, sum(share) AS total_shares FROM portfolio WHERE user_id = :user_id \
        GROUP BY stock ORDER BY stock", user_id=session.get("user_id"))
    except RuntimeError:
        return apology("welcome to the financial market")

    # Caluculate how much user's stocks are worth in total
    stock_values = 0
    latestprices = []
    for stock in stocks:
        quote = lookup(stock["stock"])
        stock_values = stock_values + quote["price"] * stock["total_shares"]

    # query database for user's current cash balance
    cash = db.execute("SELECT cash FROM users WHERE id = :id", id=session.get("user_id"))

    # Caculate user's current total asset
    total_asset = stock_values + cash[0]["cash"]
    total_asset = str(round(total_asset, 2))

    return render_template("index.html", cash=str(round(cash[0]["cash"], 2)), total_worth=total_asset)


{% extends "layout.html" %}

{% block title %}
{% endblock %}

{% block main %}

    <table border="3">
            <th>Total Shares</th>
            <th>Current Prices</th>

        {% for stock in stocks %}


        {% endfor %}

        {% for latestprice in latestprices %}


        {% endfor %}

            <td>Your Current Cash Balance: ${{cash}}</td>

            <td>Your Total Asset: ${{total_worth}}</td>


{% endblock %}

2 Answers 2


Try using the dot operator instead. {{stock.stock}} and{{stock.total_shares}}

  • Both advice helped. Thanks to you both!!!!!!!!!!
    – Alex Chan
    May 3, 2017 at 4:15
  • .....U r wlcom :) May 3, 2017 at 10:27

There is no stocks array being sent return render_template("index.html", cash=str(round(cash[0]["cash"], 2)), total_worth=total_asset). Therefore, stocks is an empty array in the html. Therefore {% for stock in stocks %} executes 0 times.


You must log in to answer this question.

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