0

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 %}
1

holdings in your python script is a dict. The {% for holding in holdings %} in the jinja template suggests a list of dicts instead.

{% holding.total + holding.cash %} is the part crashing your script. Use {{ }} instead of {% %} for those.

12
  • Yeah, I got that figured out. My latest updated code is in here: pastebin.com/Gw64H1ky, for the route, and here: pastebin.com/macgJ9AE, for the template. I'm getting an error saying that a 'str' object has no attribute 'total'. – Osman Zakir Mar 13 '17 at 14:41
  • It makes no sense to iterate that dict. Either print the dict's values without a loop, or put it in a list. – Blauelf Mar 13 '17 at 14:46
  • I need to be able to print the information for any user logged in at any given time. But I don't need a loop for that? If so, then just help me see how to fix the errors in the route itself (if any are there). – Osman Zakir Mar 13 '17 at 15:13
  • Sounds like you really want to pass a list of dicts to your template. – Blauelf Mar 13 '17 at 15:16
  • Now I have the error "jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'total'". I updated the code for the template on Pastebin, so please look again. – Osman Zakir Mar 13 '17 at 15:19

You must log in to answer this question.

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