0

In my index function, every time I try "flask run" it outputs that error (jinja2.exceptions.TemplateSyntaxError: expected name or number). Everywhere I've checked most people have been saying it's a syntax error but I can't quite catch it.

index code in application.py:

@app.route("/", methods=["GET"])
@login_required
def index():
    
    rows = db.execute("""
        SELECT symbol, SUM(shares) as total_shares
        FROM transactions
        WHERE id = :user_id
        GROUP BY symbol
        HAVING total_shares > 0;
        """, user_id=session['user_id'])
        
    quotes = {}
    grand_total = 0
    for row in rows:
        stock = lookup(row["symbol"])
        quotes.append({
            "symbol": stock["symbol"],
            "name": stock["name"],
            "shares": row["total_shares"],
            "price": usd(stock["price"]),
            "total": usd(stock["price"] * row["total_shares"])
        })
        grand_total += stock["price"] * row[total_shares]
            
    users = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id=session["user_id"])
    cash = users[0]["cash"]
    grand_total += cash
    
    return render_template("index.html", quotes=quotes, cash=usd(cash), grand_total = usd(grand_total))

index.html code:

{% block main %}
<table class="table table-striped">
        <thead>
            <tr>
                <th>Symbol</th>
                <th>Shares</th>
                <th>Price Per Share</th>
                <th>Total</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <td colspan="4"></td>
                <td>{{ grand_total }}</td>
            </tr>
        </tfoot>
        
        <tbody>
        <tbody>
            {% for quote in quotes %}
                <tr>
                    <td>{{ quote.["symbol"] }}</td>
                    <td>{{ quote.["shares"] }}</td>
                    <td>{{ quote.["price"] }}</td>
                    <td>{{ quote.["total"] }}</td>
                </tr>
            {% endfor %}
            <tr>
                <td colspan="4"></td>
                <td>{{ cash }}</td>
            </tr>
        </tbody>
    </table>
{% endblock %}

full error code:

return render_template("index.html", quotes=quotes, cash=usd(cash), grand_total = usd(grand_total))
File "/usr/local/lib/python3.9/site-packages/flask/templating.py", line 138, in render_template
ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 930, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 883, in get_template
return self._load_template(name, self.make_globals(globals))
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 857, in _load_template
template = self.loader.load(self, name, globals)
File "/usr/local/lib/python3.9/site-packages/jinja2/loaders.py", line 127, in load
code = environment.compile(source, name, filename)
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 638, in compile
self.handle_exception(source=source_hint)
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 832, in handle_exception
reraise(*rewrite_traceback_stack(source=source))
File "/usr/local/lib/python3.9/site-packages/jinja2/_compat.py", line 28, in reraise
raise value.with_traceback(tb)
File "/home/ubuntu/finance/templates/index.html", line 28, in template
<td>{{ quote.["symbol"] }}</td>
jinja2.exceptions.TemplateSyntaxError: expected name or number
0

You can't use two dictionary conventions at once. Either you can use quote.symbol or use quote["symbol"] in place of quote.["symbol"]. This should fix this particular error.

Hope this helps. If it does then please check the tickmark.

You must log in to answer this question.

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