0

I'm currently on CS50 PSET 9 Finance and stuck on the index.html portion of the assignment.

This is my code from assignment.py:

@app.route("/")
@login_required
def index():

    stocks = db.execute("SELECT * FROM stockbuy WHERE id = ?", session.get("user_id"))

    cash = db.execute("SELECT * FROM users WHERE id = ?", session.get("user_id"))
    cash_remain =  float(cash[0]["cash"])
    for stock in stocks:
        tmp_cash = (float(stock["price"]) * float(stock["quantity"]))
        cash_remain = cash_remain - tmp_cash

    cash_remain = round(cash_remain, 2)
    return render_template("index.html", currentcash=cash_remain, stocks=stocks)

My HTML:

{% extends "layout.html" %}

{% block title %}
    Buy
{% endblock %}

{% block main %}
     <table class="table table-striped">
        <thead>
            <tr>
                <th>Symbol</th>
                <th>Name</th>
                <th>Shares</th>
                <th>Price</th>
                <th>TOTAL</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <td colspan="4"></td>
                <td>{{ totalvalue }}</td>
            </tr>
        </tfoot>
        <tbody>
            {% for stock in stocks %}
                <tr>
                    <td>{{ stock["symbol"] }}</td>
                    <td>{{ stock["name"] }}</td>
                    <td>{{ stock["quantity"] }}</td>
                    <td>{{ stock["price"] }}</td>
                    <td>{{ stock["price"] * stock["quantity"] }}</td>
                </tr>
            {% endfor %}
            <tr>
                <td colspan="4">CASH</td>
                <td>{{ currentcash }}</td>
            </tr>
        </tbody>
    </table>
{% endblock %}

I have yet to implement {{ totalvalue }} as well.

Below is my Finance page:

Finance page

Below is my database for stocks: Database, showing multiple purchases

The issue I'm facing is that although i have multiple shares bought, i am unable to get the {%for%} loop in index.html to run over all the shares. I have tried to troubleshoot by passing different variables through the render_template, but I'm unsure of how to manipulate the data from db.execute through render_template and into html.

Any advice on which areas i should focus on would be much appreciated.

0

The {% for %} loop in index.html is running over all the shares (as selected by the query). The WHERE clause in the stockbuy query (WHERE id = ?) can never select both rows from that dataset because their id columns have different values.

3
  • Thanks for your reply :) Do you mean this section WHERE id = ?", session.get("user_id")? I mark all the shares with a seperate person_id (user_id) to check who they belong to.
    – Ryan Chua
    Jun 9 at 13:45
  • yes, that WHERE clause is a problem. Answer has been edited for (hopefully!) clarity. Jun 9 at 18:15
  • I understand now! Thanks a lot
    – Ryan Chua
    Jun 10 at 3:27

You must log in to answer this question.

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