【问题标题】:How to render the value of variable from a HTML page?如何从 HTML 页面呈现变量的值?
【发布时间】:2018-02-06 06:12:46
【问题描述】:

假设,如果我有字典

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

如果我不想使用 key 的 '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>

【问题讨论】:

    标签: python html python-3.x jinja2


    【解决方案1】:

    没有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>
    

    【讨论】:

      【解决方案2】:

      如果字典的位置总是在最后,你可以使用负索引。

      前:

      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
      

      【讨论】:

        猜你喜欢
        • 2013-12-03
        • 2011-03-08
        • 2021-02-09
        • 1970-01-01
        • 1970-01-01
        • 2023-03-13
        • 1970-01-01
        • 2019-01-28
        • 2018-08-04
        相关资源
        最近更新 更多