I'm working on my index page and I have two errors. For the first, I'm not sure what's wrong. It seems like it's something with my return statement but I have no idea what's wrong with it.

File "/home/ubuntu/pset9/finance/helpers.py", line 34, in decorated_function return f(*args, **kwargs) File "/home/ubuntu/pset9/finance/application.py", line 72, in index return render_template("index.html", balance=balance, portfolio=portfolio, total=total)

The second is a syntax error with jinja. I'm not sure how that happened but I'm guessing it relates to the first error.

File "/home/ubuntu/pset9/finance/templates/index.html", line 23, in template {{ usd(stock['price']) }} jinja2.exceptions.TemplateSyntaxError: expected token ',', got 'price'


def index():
    """Show portfolio of stocks"""
    # get user's balance
    command = db.execute("SELECT cash FROM users WHERE id=?", session['user_id'])
    balance = command[0]['cash']
    # get portfolio data
    portfolio = db.execute("SELECT symbol, shares FROM portfolio WHERE user_id=?", session['user_id'])
    if not portfolio:
        return apology("No stocks here")
    # get total stock value
    total = 0
    for stock in portfolio:
        price = lookup(stock['symbol'])['price']
        # add price and total value to dict
        stock['price'] = price
        stock['total'] = stock['shares']*price
        total += stock['shares']*price
    # declare new variables for index.html
    return render_template("index.html", balance=balance, portfolio=portfolio, total=total)


{% extends "layout.html" %}

{% block title %}
{% endblock %}

{% block main %}

    <div class="container">
        <table class="table">
            {% for stock in portfolio %}
                <td>{{ stock['symbol'] }}</td>
                <td>{{ stock['name'] }}</td>
                <td>{{ stock['shares] }}</td>
                <td>{{ usd(stock['price']) }}</td>
                <td>{{ usd(stock['total']) }}</td>
            {% endfor %}

{% endblock%}

I realized there is a missing quote in line 21 {{ stock['shares] }}. I fixed it now.

Also, for those wondering, in order to use {{ usd(value) }} you need to put app.jinja_env.globals.update(usd=usd) in the Custom Filter section of application.py. If you don't, you will get an error: jinja2.exceptions.UndefinedError: 'usd' is undefined.

However, doing {{ value | usd }} is much easier. Everything works now with this fix:

{% for stock in portfolio %}
    <td>{{ stock['symbol'] }}</td>
    <td>{{ stock['name'] }}</td>
    <td>{{ stock['shares'] }}</td>
    <td>{{ stock['price'] | usd }}</td>
    <td>{{ stock['total'] | usd }}</td>
{% endfor %}

Another alternative is the following as with jinja, you can access values by going into the dictionary with . rather than [] which avoids quotes altogether.

{% for stock in portfolio %}
        <td>{{ stock.symbol }}</td>
        <td>{{ stock.name }}</td>
        <td>{{ stock.shares }}</td>
        <td>{{ stock.price | usd }}</td>
        <td>{{ stock.total | usd }}</td>
{% endfor %}

You must log in to answer this question.

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