I can't seem to figure out where I've gone wrong with index. I keep running into these two errors:
After login, when directing to index - 500: Internal Server Error (raise TypeError("quote_from_bytes() expected bytes")?
After buy, when directing to index - 404: Not Found
Here is my code for index:
'''
@app.route("/")
@login_required
def index():
"""Show portfolio of stocks"""
portfolio = db.execute("SELECT stock FROM portfolio WHERE id = :id", id=session["user_id"])
# iterate through stocks in portfolio, updating current price and new value
for stocks in portfolio:
change = lookup(stocks)
price = float(change["price"])
shares= float(change["shares"])
value = float(price * shares)
db.execute("UPDATE portfolio SET price = :price AND value = :value WHERE id = :id",
price=price, value=value, id=session["user_id"])
index = db.execute("SELECT * FROM portfolio WHERE id = :id", id=session["user_id"])
stock_value = 0.00
for num in range(0, len(index)):
stock_value += index[num]["value"]
cash = db.execute("SELECT cash FROM users WHERE id = :id", id=session["user_id"])
cash_value = float(cash[0]["cash"])
total_value = stock_value + cash_value
return render_template("index.html", index=index, stock_value=stock_value, cash_value=cash_value,
total_value=total_value)
'''
Here is index.html:
'''
{% extends "layout.html" %}
{% block title %}
Index
{% endblock %}
{% block main %}
<table class="table">
<thead>
<tr>
<th scope="col">Stocks Owned</th>
<th scope="col">Shares</th>
<th scope="col">Current Price</th>
<th scope="col">Current Value</th>
</tr>
</thead>
<tbody>
{% for row in index %}
<tr>
<td>{{row.stock}}</td>
<td>{{row.shares}}</td>
<td>{{row.price}}</td>
<td>{{row.value}}</td>
</tr>
{% endfor %}
</tbody>
</table>
<table class="table">
<thead>
<tr>
<th scope="col">Cumilative Stock Value</th>
<th scope="col">Cash Value </th>
<th scope="col">Total Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{stock_value}}</td>
<td>{{cash_value}}</td>
<td>{{total_value}}</td>
</tr>
</tbody>
</table>
{% endblock %}
'''
And here are the errors for index.html I get from using Nu html Checker, but I don't understand what the misplaced non-space characters are.
Here is my table for portfolio as well, if that helps. It is storing and updating all the data correctly.
EDIT ******** Help!!!
I made the following change in def index():
# iterate through stocks in portfolio, updating current price and new value
for stocks in portfolio:
change = lookup(stocks)
price = float(change["price"])
shares = db.execute("SELECT shares FROM portfolio WHERE stock =
:stock AND id = :id", stock=stocks, id=session["user_id"])
value = float(price * shares[0]["shares"])
I also had an error in the routing for def buy(): where I had put return("/index") instead of return("/"), so I changed that. I no longer get the 404 error now when redirecting from buy, but I still get the 500: Internal Server Error: