How do I iterate zip list in jinja2 using for loop and display values in HTML table?

可紊 提交于 2019-12-13 09:54:21

问题


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))

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>

回答1:


you don't have the

<tbody> </tbody> 

tag in your page i added it and it works:

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


            <td>{{bus}}</td>
            <td>{{info}}</td>
        </tr>
        {% endfor %}
    </tbody>
</table>

here is how it looks like :




回答2:


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.



来源:https://stackoverflow.com/questions/46176785/how-do-i-iterate-zip-list-in-jinja2-using-for-loop-and-display-values-in-html-ta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!