1

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!!!

index():

@app.route("/")
@login_required
def index():
    """User's cover page."""

    # Pull past transactions from database
    try:
        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"]
        latestprices.append(quote["price"])

    # 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)

index.html:

{% extends "layout.html" %}

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

{% block main %}

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

        {% for stock in stocks %}

        <tr>
            <td>{{stock["stock"]}}</td>
            <td>{{stock["total_shares"]}}</td>
        </tr>

        {% endfor %}

        {% for latestprice in latestprices %}

        <td>
            <tr>{{latestprice}}</tr>
        </td>

        {% endfor %}

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

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

</table>

{% endblock %}
1

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

| improve this answer | |
  • Both advice helped. Thanks to you both!!!!!!!!!! – Alex Chan May 3 '17 at 4:15
  • .....U r wlcom :) – Vikrant Pradhan May 3 '17 at 10:27
1

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.

| improve this answer | |

You must log in to answer this question.

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