0

it is my first time asking here as I've tried various methods and solutions but still unable to solve this problem. I always get stuck on "buy handles valid purchase - expected to find "112.00" in page, but it wasn't found". I believe my buy section is already working and the problem is only on the index section as the results are not displayed in the homepage table (not sure why). Here are my index section in app.py and index.html file:

app.py:

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

 stocks = db.execute("SELECT shares, symbol FROM buyer WHERE id = :id", id = session["user_id"])

 for stock in stocks:
 stock = lookup(request.form.get("symbol"))
 shares = int(request.form.get("shares"))
 price = float(stock["price"])
 total = float(shares*price)

 total_money += total

 new_money = db.execute("SELECT cash FROM users WHERE id = :id", id = session["user_id"])
 total_money += new_money[0]["cash"]

 stocks = db.execute("SELECT * FROM buyer WHERE id = :id", id = session["user_id"])

 return render_template("index.html", stocks=stocks, cash = usd(new_money[0]["cash"]), total = usd(total_money))

index.html:

{% extends "layout.html" %}

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

{% block main %}
 <table class="table">
 <tr>
 <th>Symbol</th>
 <th>Shares</th>
 <th>Price</th>
 <th>Total</th>
 </tr>

        {% for stock in stocks %}
 <tr>
 <th> {{ stock["symbol"] }} </th>
 <td> {{ stock["shares"] }} </td>
 <td> {{ stock["price"] | usd }} </td>
 <td> {{ stock["total"] | usd }} </td>
 </tr>
        {% endfor %}

 <tr>
 <td>CASH</td>
 <td></td>
 <td></td>
 <td>{{ cash }}</td>
 </tr>
 <tr>
 <td></td>
 <td></td>
 <td></td>
 <td>{{ total }}</td>
 </tr>
 </table>
{% endblock %}

enter image description here

for some reason only the 'cash' and 'total' are displayed and the rest doesn't come out. I'm not really sure about jinja and flask syntaxes I just used the lecture examples and references, is there something I'm missing in the way I declare the variables? I would greatly appreciate it if somebody could help check out my code. Thanks in advance!

3
  • Is the user_id stored in the id column of the buyer table? Commented Dec 20, 2022 at 16:05
  • yep. did I make a mistake with my variable names? the buy function seems to work correctly
    – KiruFlan
    Commented Dec 24, 2022 at 14:47
  • I asked because it is a common problem that id is a key field in the "holdings" table (in this case "buyer"), and user_id is the name of the user_id column. Sorry for the misdirect. Commented Dec 24, 2022 at 19:54

1 Answer 1

0

These lines in the index function are problematic:

stock = lookup(request.form.get("symbol"))
shares = int(request.form.get("shares"))

index is called with a GET method, so request.form is the empty list. The python get method "masks" errors (by returning None if the argument is not an element of the list), but int will complain if the argument sent is None.

There is no "symbol" or "shares" in the form; that info will come from the stocks list returned by execute. And notice that the calculated fields price and total are not being added to the stocks list, so will not be sent to the html.

You must log in to answer this question.

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