0

I am in desperate need of help, since the index method is the last method I have left to finish for this pset! So far, my index code works partially - as in, only the values I passed in from my portfolio database (symbol and name) are shown correctly, while the last three fields I tried to iterate over, (shares, price, and total) are not outputted. Can you please help me spot my error in the code below? I believe it must be in either my while loop, my html for loop, or both...

def index():

    rows = db.execute("SELECT symbol, name, sum(quantity) FROM portfolio WHERE id = :user_id GROUP BY symbol HAVING sum(quantity) > 0 ORDER BY symbol", user_id=session["user_id"])
    if not rows:
        return render_template("emptyindex.html")
    i = -1
    shares = []
    price = []
    total = []
    while(True):
        i = i + 1
        try:
            result = lookup(rows[i]["symbol"])
            shares.append(int(rows[i]["sum(quantity)"]))
            price.append(float(result["price"]))
            total.append(int(rows[i]["sum(quantity)"])*price[i])
        except:
            break

    return render_template("index.html", rows=rows, price=price, total=total, shares=shares)

Here's my html

{% extends "layout.html" %}

{% block title %}
    History
{% 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>
        <tbody>
            {% for row in rows %}
            <tr>
                <td>{{ row.symbol }}</td>
                <td>{{ row.name }}</td>
                <td>{{ shares[row] }}</td>
                <td>${{ price[row] }}</td>
                <td>${{ total[row] }}</td>
            </tr>
            {%endfor%}
        </tbody>
    </table>

{% endblock %}
  • What is the length of rows, shares, price, and total right before you render the index? – ChrisG Jul 15 '17 at 10:00
  • @ChrisG Well considering there are two unique stocks with positive quantities in my database, the length of rows is 2, and the shares, price, and total arrays should also have 2 items appended to each of them as well. So why is it not printing in the table? The problem is that I can't even debug - debug50 only works on c programs... – Jason_V Jul 15 '17 at 16:08
0

Realized my problem through trial and error! It was actually mostly in the html. Apparently, I can't use "row" as an array index in my jinja for loop, as row is a dict, not an integer. Rather, I had to initialize an integer variable using the jinja {% set... %} and increment it on every iteration of the for loop. I used this integer as my array index, and it solved the problem!

You must log in to answer this question.

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