0

I'm stuck with pset9 Finance... When I try to sell a quote and return to index.html, I have an error with :

File "/home/ubuntu/pset9/finance/templates/index.html", line 1, in top-level template code
{% extends "layout.html" %}
File "/home/ubuntu/pset9/finance/templates/layout.html", line 62, in top-level template code
{% block main %}{% endblock %}
File "/home/ubuntu/pset9/finance/templates/index.html", line 33, in block "main"
<td class="table-dark">{{ totalCash | usd}}</td>
File "/home/ubuntu/pset9/finance/helpers.py", line 64, in usd
return f"${value:,.2f}"
TypeError: unsupported format string passed to Undefined.__format__

I don't understand what it means. I'm pretty sure that the return value which use usd is a float (I casted it).

Here is the part of index.html which is problematic :

<tbody>
    {% for stock in stocks %}
    <tr>
        <td>{{ stock["name"] }}</td>
        <td>{{ stock["symbol"] }}</td>
        <td>{{ stock["price"] | usd }}</td>
        <td>{{ stock["sumed"] }}</td>
        <td>{{ (stock["sumed"] * stock["price"]) | usd }}</td>
    </tr>
    {% endfor %}
    <tr>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td class="table-dark">{{ totalCash | usd }}</td>
    </tr>
</tbody>

And the function linked to it:

@app.route("/")
@login_required
def index():
    """Show portfolio of stocks"""
    # Get user id
    currentUser = session["user_id"]

    # Get stocks from db
    stocks = db.execute("SELECT name, symbol, SUM(shares) AS sumed, price FROM actions WHERE user_id=? and type=? GROUP BY symbol", currentUser, "buy")

    # Get user cash from db
    cash = db.execute("SELECT cash FROM users WHERE id=?", currentUser)
    totalCash = float(cash[0]["cash"])
    totalCash = round(totalCash, 2)

    return render_template("index.html", stocks = stocks, totalCash = totalCash)

Last but not least, here is usd function from helpers.py:

def usd(value):
    """Format value as USD."""
    return f"${value:,.2f}"

Thank you in advance for your time

You must log in to answer this question.

Browse other questions tagged .