0

Im getting error 500 messages, when it should instead show the index page after buying or when clicking on history and sell.

This is my code for index().

def index():
    """Show portfolio of stocks"""

    # look up the current user
    users = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id=session["user_id"])
    stocks = db.execute(
        "SELECT symbol, SUM(shares) as total_shares FROM transactions WHERE user_id = :user_id GROUP BY symbol HAVING total_shares > 0", user_id=session["user_id"])
    quotes = {}

    for stock in stocks:
        quotes[stock["symbol"]] = lookup(stock["symbol"])

    cash_remaining = users[0]["cash"]
    total = cash_remaining

    return render_template("portfolio.html", quotes=quotes, stocks=stocks, total=total, cash_remaining=cash_remaining)

I cant seem to get it working. Any help would be appreciated thank you

This is the traceback I get after i log in or click buy on selected stock and number of stock (for which i get the 500 error):

INFO:werkzeug:192.168.83.137 - - [27/Aug/2020 10:37:37] "GET / HTTP/1.0" 500 -
INFO:werkzeug:192.168.83.137 - - [27/Aug/2020 10:37:38] "GET /static/styles.css HTTP/1.0" 200 -
INFO:werkzeug:192.168.83.137 - - [27/Aug/2020 10:37:40] "GET /buy HTTP/1.0" 200 -
INFO:werkzeug:192.168.83.137 - - [27/Aug/2020 10:37:40] "GET /static/styles.css HTTP/1.0" 200 -
DEBUG:cs50:SELECT cash FROM users WHERE id = 1
DEBUG:cs50:UPDATE users SET cash = cash - 1010.6 WHERE id = 1
DEBUG:cs50:INSERT INTO transactions (user_id, symbol, shares, price_per_share) VALUES(1, 'AAPL', 2, 505.3)
ERROR:application:Exception on /buy [POST]
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/ubuntu/finance/finance/helpers.py", line 32, in decorated_function
    return f(*args, **kwargs)
  File "/home/ubuntu/finance/finance/application.py", line 98, in buy
    price=price_per_share)
  File "/usr/local/lib/python3.7/site-packages/cs50/sql.py", line 21, in decorator
    return f(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/cs50/sql.py", line 372, in execute
    raise e
RuntimeError: no such table: transactions
INFO:werkzeug:192.168.83.137 - - [27/Aug/2020 10:37:45] "POST /buy HTTP/1.0" 500 -
INFO:werkzeug:192.168.83.137 - - [27/Aug/2020 10:37:45] "GET /static/styles.css HTTP/1.0" 200 -
DEBUG:cs50:SELECT symbol, shares, price_per_share, created_at FROM transactions WHERE user_id = 1 ORDER BY created_at ASC
ERROR:application:Exception on /history [GET]
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/ubuntu/finance/finance/helpers.py", line 32, in decorated_function
    return f(*args, **kwargs)
  File "/home/ubuntu/finance/finance/application.py", line 114, in history
    "SELECT symbol, shares, price_per_share, created_at FROM transactions WHERE user_id = :user_id ORDER BY created_at ASC", user_id=session["user_id"])
  File "/usr/local/lib/python3.7/site-packages/cs50/sql.py", line 21, in decorator
    return f(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/cs50/sql.py", line 372, in execute
    raise e
RuntimeError: no such table: transactions
INFO:werkzeug:192.168.83.137 - - [27/Aug/2020 10:37:51] "GET /history HTTP/1.0" 500 -
INFO:werkzeug:192.168.83.137 - - [27/Aug/2020 10:37:51] "GET /static/styles.css HTTP/1.0" 200 -
  • Please post the stack trace from the terminal as well. – Ashen Gunaratne Aug 26 at 3:46
1

@Eric Without the traceback the only speculation I can advocate is in correlation with the line

cash_remaining = users[0]["cash"]

which will yield an KeyError thereby causing a server error upon users being empty due to no allocated stocks for the respective user_id. Please update the post with the traceback to aid a finer troubleshoot of the issue.

| improve this answer | |
  • Just posted and thank you – Eric P. Aug 27 at 10:43
  • I also updated the index function but it still doesnt work – Eric P. Aug 27 at 10:49
  • @EricP. There exists no table named transactions according to exception excerpt RuntimeError: no such table: transactions; did you create the table properly? You can use the sqlite3 command .tables to list all existing tables within a SQLite instance. – Ashen Gunaratne Aug 27 at 15:41

You must log in to answer this question.

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