如何使用for循环并在HTML表中显示值来迭代jinja2中的zip列表? [英] How do I iterate zip list in jinja2 using for loop and display values in HTML table?

查看:155
本文介绍了如何使用for循环并在HTML表中显示值来迭代jinja2中的zip列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图迭代jinja2中的zip列表并在HTML表中显示值,但是每次尝试空白页都失败了,但是,我可以像下面这样在Unordered列表中显示值.

I was trying to iterate zip list in jinja2 and display values in HTML table but failed at every single try with a blank page, however, I can display values in the Unordered list like as follows.

<ul>
      {% for bus, info in jnjbus_info %}
       <li>{{bus}}</li>
       <li>{{info}}</li>
{% endfor %}
</ul>

这是我的烧瓶/函数,我在其中将值传递给模板:

This is my flask/function where I passing values to template:

@app.route('/busses')
def busses():
    bus_type = ['AC', 'NON-AC', 'Sleeper', 'NON-Sleeper']   
    bus_info = ['1010', '2020', '3030', '4040']   
    return render_template('busses.html', jnjbus_info=zip(bus_type, bus_info))

我正在渲染名为busses.html的模板 这是脚本:

I'm rendering template called busses.html Here's the script:

    <table style="width:100%">
        <tr>
            <th>Bus Type</th>
            <th>Bus Information</th>
        </tr>
            {% for bus, info  in jnjbus_info %}
                <tr>    
                <td>{{bus}}</td>
                <td>{{info}}</td>               
               </tr>
           {% endfor %} 
    </table>

推荐答案

此问题中提供的代码没有错误,但是,问题是由port=5000引起的,也可能是浏览器cache.解决该错误时,我编写了一个类似的脚本 py

The code provided in this question is bug-free, however, the problem caused by port=5000 and may be browser cache as well. While tackling with the bug I have written a similar script py and html and screen shot .and ran on port=8888 which works like charm. Note : consider running the same application on different ports and clearing browser cache.

这篇关于如何使用for循环并在HTML表中显示值来迭代jinja2中的zip列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆