Reputation: 666
I try to make few tables with data import on web page! I'm using Flask-Jinja with Python. For example I have this double list:
list = [[{'pricebb': 1199.99, 'model': 'model1', 'pricebh': 1199, 'pricea': 1299}, {'pricebb': 1199.99, 'model': 'model2', 'pricebh': 1199, 'pricea': 1299}, {'pricebb': 1499.99, 'model': 'model3', 'pricebh': 1499, 'pricea': 1599}], [{'pricebb': 399.99, 'model': 'model4', 'pricebh': 459, 'pricea': 499}, {'pricebb': 599.99, 'model': 'model5', 'pricebh': 669, 'pricea': 699}, {'pricebb': None, 'model': 'model6', 'pricebh': 899, 'pricea': 999}]]
I want to separate it with loop and create 2 different tables on one page. If I use Python with list[0] I get 1-st sublist, but when I try it on Flask:
{% for post in posts %}
<p>{{post[0]}}</p>
{% endfor %}
It return to me - model1 and model4) Why its happened? How can I get 1-st sublist from list? Have you any idea !? Thank you!
run1()
list= sqlselect()# here is array from DB
a = list # its a list sample that I posted above
@FlaskApp2.route('/', methods=('Get', 'Post'))
@FlaskApp2.route('/index')
def index():
user = {'nickname': 'parser'} # fake user
return render_template("index.html",
title='Web',
user=user,
posts=list, describe=des)
Here is index.html:
<table>
<caption>products compare list ({{item}})</caption>
<thead>
<tr>
<th>qqq.com</th>
<th>Price</th>
<th>qqq.com</th>
<th>qqq.com</th>
</tr>
</thead>
{% for post in posts %}
<p>{{post[0]}}</p>
{% endfor %}
</table>
Upvotes: 3
Views: 5368
Reputation: 9110
You can access internal lists by posts[0]
- first list, and posts[1]
-
second list (you never should name a variable with a reserved word in python - don't name your var list
, but mylist
). So to access the elements for the first list you should use:
{% for post in posts[0] %}
{% for key, value in post.items %}
<p>{{ key }}: {{ value }}</p>
{% endfor %}
and for the second list:
{% for post in posts[1] %}
{% for key, value in post.items %}
<p>{{ key }}: {{ value }}</p>
{% endfor %}
Upvotes: 5