I have an exception thrown from a template, which reads like this: "jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'holding'. Jinja was looking for the following tags: 'endfor' or 'else'. The innermost block that needs to be closed is 'for'.".
I also had an error about multiplying a list with a non-list, and I'm not sure if my solution for that is correct. So I'll post my code for the index
route here along with the template code.
route:
@app.route("/")
@login_required
def index():
user_cash = db.execute("SELECT cash FROM users WHERE id=:id", id=session["user_id"])
stock_symbol = db.execute("SELECT stock_symbol FROM transactions WHERE user_id=:user_id", user_id=session["user_id"])
stock = lookup(stock_symbol[0]["stock_symbol"])
if not stock:
apology("symbol is invalid or something else went wrong")
no_of_shares = db.execute("SELECT no_of_shares FROM transactions WHERE user_id=:user_id", user_id=session["user_id"])
holdings = {
"stock_name": stock["name"],
"stock_symbol": stock["symbol"],
"shares": no_of_shares,
"stock_price": stock["price"],
"total": stock["price"] * no_of_shares[0]["no_of_shares"],
"cash": user_cash
}
return render_template("index.html", holdings=holdings)
template:
{% extends "layout.html" %}
{% block title %}
Portfolio
{% endblock %}
{% block main %}
<table class="table table-striped">
<thead>
<tr>
<th>Symbol</th>
<th>Name</th>
<th>Shares</th>
<th>Price</th>
<th>TOTAL</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="4"></td>
{% for holding in holdings %}
<td>{% holding.total + holding.cash %}</td>
{% endfor %}
</tr>
</tfoot>
<tbody>
<tr>
{% for holding in holdings %}
<td>{% holding.stock_symbol %}</td>
<td>{% holding.stock_name %}</td>
<td>{% holding.shares %}</td>
<td>{% holding.stock_price %}</td>
<td>{% holding.total %}</td>
{% endfor %}
</tr>
<tr>
<td colspan="4"></td>
{% for holding in holdings %}
<td>{% holding.cash %}</td>
{% endfor %}
</tr>
</tbody>
</table>
{% endblock %}