jinja2,有一些循环和两个表对象的问题

2024-09-26 22:11:06 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的密码:

<!-- Loop thru Table Macro begins--> {% macro loopThruTable(currentParent, prod)%} <li> {{prod.category_name}} </li> {% set currentParent = prod.id %} {{ checkParent(currentParent) }} <ul> <table> <tr> <th>Service</th> <th>Price</th> </tr> {{ checkParentService(currentParent)}} </table> </ul> {% endmacro %} <!-- Loop thru Table Macro ends--> <!-- CheckParent Macro begin--> {% macro checkParent(currentParent) %} {% for prod in myProd %} {% if prod.parent_category_id == currentParent %} <ul> {{ loopThruTable(currentParent, prod) }} </ul> {% endif %} {% endfor %} {% endmacro %} <!-- checkParent Macro Ends--> <!-- CheckParentService Macro begin--> {% macro checkParentService(currentParent) %} {% for serv in myServ %} {% if serv.parent_category_id == currentParent %} <tr> {{ loopThruTableService(currentParent, serv.name) }} {{ loopThruTableService(currentParent, serv.price) }} </tr> {% endif %} {% endfor %} {% endmacro %} <!-- checkParent Macro Ends--> <!-- Loop thru Table Macro begins--> {% macro loopThruTableService(currentParent, blah) %} <td>{{blah}}</td> {% endmacro %} <!-- Loop thru Table Macro ends--> {% extends 'admin/master.html' %} {% block body %} <ul> {% set parent = 0 %} {{ checkParent(parent) }} </ul> {% endblock %}

无论出于何种目的,它都基本正确地运行着,但是它在每个循环的末尾添加了一个额外的表

enter image description here

正如您在底部看到的,有一个“ServicePrice”将每个类别分开

enter image description here

这是另一个例子。我知道这与我的循环有关,但就jinja2而言,我不知道是否有一个方法存在,我检查父对象是否在我的对象中“存在”

谢谢你


Tags: looptableprodultrparentmacrocategory
1条回答
网友
1楼 · 发布于 2024-09-26 22:11:06

我想出来了

我不得不在loopThruTable宏中添加一个额外的for循环和if语句

现在看起来是这样的:

{% macro loopThruTable(currentParent, prod)%} <li> {{prod.category_name}} </li> {% set currentParent = prod.id %} {{ checkParent(currentParent) }} {% for serv in myServ %} {% if serv.parent_category_id == currentParent %} <table> <tr> <th>Service</th> <th>Price</th> </tr> {{ checkParentService(currentParent)}} </table> {% endif %} {% endfor %} {% endmacro %}

我还是不太清楚为什么这样行。但它是有效的

相关问题 更多 >

    热门问题