0

stuck trying a for loop in jinja. I have a list/array called "priceNow" with 4 items "prices" in there. But i can't seem to get it to display in html via Jinja.

1) Here is the code that doesn't work, but if it's like any other for loop(like in c) it should iterate through share and give the values 0, 1, 2, 3.. right?:

 {% for share in sharesOwned %}
    <tr>
        <td>{{ share.symbol }}</td>
        <td>{{ share.name }}</td>
        <td>{{ share.quanties }}</td>
        <td>{{ priceNow[share] }}</td>
    </tr>

{% endfor %}

enter image description here

However, when I do specify which list/array like priceNow[2], it displays the prices correctly.

     {% for share in sharesOwned %}
        <tr>
            <td>{{ share.symbol }}</td>
            <td>{{ share.name }}</td>
            <td>{{ share.quanties }}</td>
            <td>{{ priceNow[1] }}</td>
        </tr>

    {% endfor %}

enter image description here

2) More from a design front, how do i get another column of data appended to the current table? I tried this:

 {% for share in sharesOwned %}
        <tr>
            <td>{{ share.symbol }}</td>
            <td>{{ share.name }}</td>
            <td>{{ share.quanties }}</td>
        </tr>

    {% endfor %}

    {% for priceNow in seq %}
        <td>{{ priceNow }}</td>
    {% endfor %}

but it adds another row instead of column.. :( how do i just add it to current price column? enter image description here

1 Answer 1

1

the share in for share in sharesOwnedis the object itself so when u try to use something like priceNow[share] , it doesnt make sense. if you have a diff pricenow list then u can iterate both list using a function called zip().

It works something like this:

{% for share, price in share_price %}
    <tr>enter code here
        <td>{{ share.symbol }}</td>
        <td>{{ share.name }}</td>
        <td>{{ share.quanties }}</td>
        <td>{{ price }}</td>
    </tr>

{% endfor %}

Here share_price is just a arbitary name i gave for combination of the two list.

Now in index route you can add

return render_template ("index.html", share_price = zip(nameofthesharelist, priceNow));

1
  • Thanks for that!
    – nvs0000
    May 31, 2017 at 18:12

You must log in to answer this question.

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