tonado框架的列表嵌套 (template中for循环嵌套)
学习了tonado框架,渐渐开始明白模板的转换,以后肯定还会遇到很多问题。。。
功能描述:
页面显示读取的数据库父导航名称,再通过嵌套列表将子导航名称和地址查询出来,返回到页面显示,由于页面有多个父导航,父导航下又有多个子导航,所以template采用for循环嵌套,model里有两个类。两个列表关系分如下:
[pnav1, pnav2, pnav3]
pnav1.pnav_name = "父导航名1"
pnav1.cnavs = [cnav1, cnav2, cnav3]
cnav1.cnav_name = "子导航名1"
cnav1.cnav_addr = "子导航映射地址1"
cnav2.cnav_name = "子导航名2"
cnav2.cnav_addr = "子导航映射地址2"
pnav2.pnav_name = "父导航名2"
pnav2.cnavs = [cnav1, cnav2, cnav3]
cnav1.cnav_name = "父导航名2的子导航名1"
cnav1.cnav_addr = "父导航名2子导航映射地址1"
cnav2.cnav_name = "父导航名2子导航名2"
cnav2.cnav_addr = "父导航名2子导航映射地址2"
tonado-template代码片段:
{%for pnav in pnavlist %}
<div class="panel-heading">
<h4 class="panel-title"> <a class="a" data-toggle="collapse" data-parent="#accordion" href="#collapseThree"> <span class="glyphicon glyphicon-plus-sign span_icon" aria-hidden="true" data-toggle="collapse" data-parent="#accordion" ></span> </a>{{pnav.pnav_name}}</h4>
</div>
<div id="collapseTwo" class="panel-collapse collapse in">
<div class="panel-body">
{%for cnav in pnav.cnavs %}
<ul class="list-unstyled panel-title1">
<li>
<span class="glyphicon glyphicon-minus-sign span_icon" aria-hidden="true" data-toggle="collapse" data-parent="#accordion" >
</span>
<a href="{{cnav.cnav_addr}}" class="active">{{cnav.cnav_name}}
</a>
</li>
</ul>
{%end%}
</div>
</div>
{%end%}
model代码:
class pnav
def __init__(self, pnav_name):
self.pnav_name = pnav_name
self.cnavs = []
class cnav
def __init__(self, cnav_name, cnav_addr):
self.cnav_name = cnav_name
self.cnav_addr = cnav_addr
努力吧,为了媳妇儿,为了家。。。