如何使用Jinja2将列表列表呈现为HTML

时间:2019-04-26 11:09:45

标签: python jinja2

我正在尝试通过python3将足球比赛数据收集到一个表中,将jinja2收集到一个样式化的HTML模板页面中。我只是编程的新手,在传输数据时可能会犯一些错误。

我试图将结果打印出来看起来像

mylist=[[['2019-04-27', '03:00'], ['liverpool', 'liverpool', 'EP', 'tag'], ['live']],
[['2019-04-27', '10:00'], ['GSW', 'LAC', 'NBA', 'tags', 'tags'], ['live1', 'live2']],
[['2019-04-27', '19:30'], ['hotspurs', 'WH', 'EP'], ['live']]]

但是当我尝试使用jinja2渲染HTML时,例如     print(template.render(mylist)) 它显示

Traceback (most recent call last):
  File "Z:/学习/pythonLearning/zhibo8_v4.py", line 57, in <module>
    print(template.render(showListReady))
  File "C:\Program Files\Python37\lib\site-packages\jinja2\asyncsupport.py", line 76, in render
    return original_render(self, *args, **kwargs)
  File "C:\Program Files\Python37\lib\site-packages\jinja2\environment.py", line 1003, in render
    vars = dict(*args, **kwargs)
ValueError: dictionary update sequence element #0 has length 3; 2 is required

我认为我的HTML模板可能有问题,例如

        <tbody>
        {% for game in showListReady %}
        {% for date, time, teamA, teamB, tags, broadcast in game %}
        <tr>
            <td class="firstColumn">
                <div class="dateClass">${date}</div>
                <div class="timeClass">${time}</div>
            </td>
            <td class="secondColumn">
                <span class="teamName">${teamA}</span>
                <span><img src="images/xxxx.png"></span>
                <span class="VS">VS</span>
                <span><img src="images/xxxx.png"></span>
                <span class="teamName">${teamB}</span>
                <span class="leagueName">${tags}</span>
            </td>
            <td class="thirdColumn">
                <span class="broadcast">${broadcast}</span>
              </td>
        </tr>
        {% endfor %}
        {% endfor %}

希望有人可以给我一些建议

1 个答案:

答案 0 :(得分:0)

也许尝试:

print(template.render(showListReady=showListReady))