0

Users can buy stock successfully and see their portfolio (i.e. index), but after a user buys a stock I send them to a bought page with info just on the stock purchased, along with their remaining cash.

While things work fine for users that have purchased shares before, I've issues with new users.

For instance, this code in application.py "works" (the page appears without issue but I want to show the cash amount after it was updated, not before):

        # Check user's cash amount
        cash = db.execute("SELECT cash FROM users WHERE id = :id", id=session["user_id"])

        cash_ready = cash[0]["cash"]

        # Check if user already owns that stock
        user_shares = db.execute("SELECT shares FROM portfolio WHERE id = :id AND symbol = :symbol", id=session["user_id"], symbol=quoted["symbol"])

       if cash_ready >= order:

            #subtract from cash (shares*symbol.price)
            db.execute("UPDATE users SET cash = cash - :order WHERE id = :id", id=session["user_id"], order=order)

....skipping over code that's probably not relevant....

        #Show bought page when purchase is successful
        return render_template("bought.html", quoted=quoted, amount_requested=amount_requested, order=order, cash=cash[0]["cash"])

However, if I try to select the cash again once the cash was updated, I get "RuntimeError: unsupported value". I'm not understanding why this is occurring. The lines of code I'm adding (after updating the cash so it's less the order):

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

Here's my updated render_template line:

return render_template("bought.html", quoted=quoted, amount_requested=amount_requested, order=order, cash=cash_updated[0]["cash_updated"])

Here's the bought html page that is shown after the purchase is successful:

{% extends "layout.html" %}

{% block title %}
    Purchase Results
{% endblock %}

{% block main %}
<p>  {{amount_requested}} share(s) of {{quoted.name}} ({{quoted.symbol}}) bought at {{quoted.price | usd}} for a grand total of {{order | usd}}. Remaining cash is {{cash | usd}}.</p>

{% endblock %}

As requested, here's the traceback:

Traceback (most recent call last):

  File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__

    return self.wsgi_app(environ, start_response)
  File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)

  File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)

  File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value

  File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()

  File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)

  File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)

  File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value

  File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()

  File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)

  File "/home/ubuntu/workspace/pset7/finance/helpers.py", line 33, in decorated_function
    return f(*args, **kwargs)

  File "/home/ubuntu/workspace/pset7/finance/application.py", line 143, in buy
    cash_now = cash_updated[0]["cash_updated"]
KeyError: 'cash_updated'
1
  • Can you edit your post to show the terminal traceback from the point of the buy page loading to the end?
    – curiouskiwi
    Nov 10 '18 at 0:49
0
cash_updated = db.execute("SELECT cash FROM users WHERE id = :id", id=session["user_id"])
cash_now = cash_updated[0]["cash_updated"]

This cash_updated list has no dict element with "key" = "cash_updated". The render_template has the same "problem".

1
  • Thanks! It finally clicked to me when I went back to the first time I selected cash and realized "cash_updated" is not in my database...
    – MEM
    Nov 12 '18 at 18:47

You must log in to answer this question.

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