0

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

enter image description here

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.

enter image description here

Here is my table for portfolio as well, if that helps. It is storing and updating all the data correctly.

enter image description here

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:

enter image description here

enter image description here

1 Answer 1

1

--AFTER EDIT--

This line change = lookup(stocks) is a problem. execute returns a list of dictionaries, therefore stocks is a dictionary, therefore lookup is choking, I mean, producing the error message.

This line shares= float(change["shares"]) is a problem, since lookup does not return a key named "shares". I can't say for sure if that has anything to do with the quote_from_bytes error; I'd need to see more of the previous lines in the traceback, probably back to the last db call (ie DEBUG:CS50 line). An internet search for quote_from_bytes returns lots of interesting posts, including this one from this forum.

Nu HTML won't "work", ie give meaningful results, on the raw template. It has the be the actual HTML. You can get that with a right-click, View page source; then copy/paste into the validator.

This "After buy, when directing to index - 404: Not Found" is (likely) a problem in the return in the buy route, eg a nonexistent route name; return redirect("/index") will cause that. Review the login route for the correct syntax.

3
  • Thanks for the help, Dino. I changed that line, and you were right about the return in the buy route as well so I changed that also. I no longer get the 404 error but I get 500: Internal Server Error still. I checked out the post you linked but I still can't see what the issue is. I made an edit in the post with a picture of the full traceback
    – JoshGrant5
    Commented Jul 1, 2020 at 22:12
  • Answer updated. The additional traceback info pointed the finger at lookup. Commented Jul 2, 2020 at 0:24
  • That was it. Everything works now, thanks so much!!
    – JoshGrant5
    Commented Jul 2, 2020 at 0:33

You must log in to answer this question.

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