如何从HTML页面呈现变量的值?

时间:2018-02-06 06:12:47

标签: python html python-3.x jinja2

假设,如果我有字典

game = [{'points': '534', 'Team': 'Ireland', 'Rating': '36', 'Matches': '15'}, {'points': '5146', 'Team': 'England Women', 'Rating': '129', 'Matches': '40'}, {'points': '5898', 'Team': 'Australia Women', 'Rating': '128', 'Matches': '46'},{'date-updated': '05 February 2018', 'match-type': "ICC Women's Championship"}]

如果我不想使用密钥'points''Team''Rating''Matches',我可以使用for循环

{% for team in game %}
    <tr class="table table-bordered">
        <td>{{ team["Team"]}}</td>    
        <td>{{ team['Matches'] }}</td>
        <td>{{ team['points'] }}</td>
        <td>{{ team['Rating'] }}</td>
{% endif%}

我可以打印这些键的值。

如何在不使用for循环的情况下呈现date-updatedmatch-type的键值,因为只有一组字典存在。

我试着把它称为贝娄,但它没有打印任何东西

<tr>
    <th class="bg-danger text-lg-left">{{game['date-updated']}} </th>
    <th class="bg-danger text-lg-right">{{game['match-type']}}</th>
</tr>

2 个答案:

答案 0 :(得分:0)

没有for循环,你需要知道数组的索引,

按照你的例子

<tr>
    <th class="bg-danger text-lg-left">{{game[3]['date-updated']}} </th>
    <th class="bg-danger text-lg-right">{{game[3]['match-type']}}</th>
</tr>

答案 1 :(得分:0)

如果词典的位置始终位于最后一个位置,则可以使用否定索引。

<强> EX:

game = [{'points': '534', 'Team': 'Ireland', 'Rating': '36', 'Matches': '15'}, {'points': '5146', 'Team': 'England Women', 'Rating': '129', 'Matches': '40'}, {'points': '5898', 'Team': 'Australia Women', 'Rating': '128', 'Matches': '46'},{'date-updated': '05 February 2018', 'match-type': "ICC Women's Championship"}]

print game[-1]['date-updated']
print game[-1]['match-type']

<强>输出:

05 February 2018
ICC Women's Championship