简体   繁体   English

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

[英]How to render a list of lists to HTML with Jinja2

I'm trying to collect some soccer game data to a single table by python3, jinja2 to a stylized HTML template page. 我试图通过python3,jinja2将一些足球游戏数据收集到单个表格中,然后再添加到程式化的HTML模板页面。 I'm just beginner in programming and I may have made some mistake on transferring the data. 我只是编程的初学者,我可能在传输数据时犯了一些错误。

I tried to print the result out it seems ok like 我试图将结果打印出来似乎没问题

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

but when I try to render HTML with jinja2, like print(template.render(mylist)) it shows 但是当我尝试使用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

I think it could be something wrong in my HTML template, like 我认为我的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 %}

hope someone could give me some advice, please 希望有人能给我一些建议

也许试试:

print(template.render(showListReady=showListReady)) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM