假设列表为a1=[ ],a2=[ ],a3=[ ],a4=[ ],a5=[ ]
他们都将拥有动态值,如下所示:
a1=[]
a2=[]
a3=[{'car':'bez','city':'la','aero':'vaar'}]
a4=[]
a5=[{'car':'tez','city':'pa','aero':'vawear'}]
我想只渲染具有值的那些,如本例a3和a5那么我如何检查具有值的列表然后将其呈现给模板card.html
<table>
<tbody>
<thead>
<th>car</th>
<th>city</th>
<th>aero</th>
</thead>
{{for key in a3 /a5}}
<td>key[car]</td>
<td>key[city]</td>
<td>key[aero]</td>
</tbody>
</table>
并创建一个获取特定对象表的链接,我该怎么做?
<html>
<body>
<p><a hr ef="/card">card</a></p> """which gives a3 value"""
<p><a href="/card">card</a></p>"""which gives a5 value"""
</body>
</html>
答案 0 :(得分:1)
您可以组合所有列表,然后迭代它。
示例:强>
from jinja2 import Template
myString = """<table>
<tbody>
<thead>
<th>car</th>
<th>city</th>
<th>aero</th>
</thead>
{% for key in data %}
<td>{{ key['car'] }}</td>
<td>{{ key['city'] }}</td>
<td>{{ key['aero'] }}</td>
{% endfor %}
</tbody>
</table>"""
a1=[]
a2=[]
a3=[{'car':'bez','city':'la','aero':'vaar'}]
a4=[]
a5=[{'car':'tez','city':'pa','aero':'vawear'}]
d = (a1 + a2 + a3 + a4 + a5)
data = Template(myString).render({'data': d})
print(data)
<强>输出:强>
<table>
<tbody>
<thead>
<th>car</th>
<th>city</th>
<th>aero</th>
</thead>
<td>bez</td>
<td>la</td>
<td>vaar</td>
<td>tez</td>
<td>pa</td>
<td>vawear</td>
</tbody>
</table>