2017-06-04 82 views
0

我嘗試使用網頁上的數據導入製作幾張表格!我用Python來使用Flask-Jinja。 例如,我有這樣的雙重名單:如何在Flask-Jinja列表中使用列表蟒蛇Python

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}]] 

我想將它與環分離並在一個頁面上創建2個不同的表。 如果我使用Python與列表[0]我得到1-ST子表,但是當我嘗試在瓶:

{% for post in posts %} 
    <p>{{post[0]}}</p> 
     {% endfor %} 

還給我 - MODEL1和model4)爲什麼它的發生?我如何從列表中獲得1-st子列表?你有什麼想法!?謝謝!

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) 

下面是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> 
+0

可以共享整個程序 – Rednivrug

+0

更新........ – TheRutubeify

回答

1

您可以通過posts[0]訪問內部列表 - 第一個列表,並​​- 第二列表(你不應該名稱與蟒蛇的保留字變量 - 請勿指定您的變種list,但mylist)。因此,要訪問的元素爲第一個列表,你應該使用:

{% for post in posts[0] %} 
    {% for key, value in post.items %} 
    <p>{{ key }}: {{ value }}</p> 
{% endfor %} 

和第二列表:

{% for post in posts[1] %} 
    {% for key, value in post.items %} 
    <p>{{ key }}: {{ value }}</p> 
{% endfor %} 
+0

謝謝你,它的工作! – TheRutubeify

+1

@TheRutubeify很高興它的工作,現在你可以標記答案爲接受結束這個問題。 – doru