一個特定的字典項我有一個靜態dictionary
:的Python瓶:訪問模板
urls = {
'foo': 'http://example.com',
'bar': 'http://example2.com',
'test': 'http://example3.com'}
然後,我有一個元組列表。每個tuple
包含以前字典的一個關鍵:
myList = [('bar', 0.9),('test', 0.7),('foo', 0.5)]
我希望把我的模板中的相關網址爲按降序每個密鑰字符串,因爲它們在列表中(與reverse = True
集創建in this way明顯) 。
在我已經試過這一點,但是,如預期的模板,這是行不通的:
{% for i in myList %}
<tr>
<a href= " {{ urls[i[0]] }} ">
<td>
{{ i[0] }}
</td>
</a>
</tr>
{% endfor %}
所以,我怎麼能訪問dictionary
元素?
你得到了什麼錯誤的,什麼是你的'render_template'代碼看起來像? – Doobeh
沒有錯誤,只是超鏈接不工作,我只是文本 – accand
我看不到你的字典格式正確。我已經初始化它,像這樣:'url = {0 {0 {0}'''''':'http://example.com', 'bar':'http://example2.com', 'test':'http: //example.com', }' – ferdy