Python 如何使用TAL和嵌套集合列表在变色龙模板中创建树(修改的前序树遍历)
如前所述,我正在使用嵌套集层次结构或修改的前序树遍历MPTT算法,我很难理解如何将深度和类别名称转换为变色龙模板,以使用and显示嵌套树 我有一个dict,其中包含所有类别的深度和类别名称信息,如下所示:Python 如何使用TAL和嵌套集合列表在变色龙模板中创建树(修改的前序树遍历),python,tree,mptt,chameleon,Python,Tree,Mptt,Chameleon,如前所述,我正在使用嵌套集层次结构或修改的前序树遍历MPTT算法,我很难理解如何将深度和类别名称转换为变色龙模板,以使用and显示嵌套树 我有一个dict,其中包含所有类别的深度和类别名称信息,如下所示: [{'depth': 1L, 'name': 'CategoryA'}, {'depth': 2L, 'name': 'CategoryB', ...] 根据这一点,用Python打印此信息的方法是: category_ul = "" current_depth = -1 category
[{'depth': 1L, 'name': 'CategoryA'}, {'depth': 2L, 'name': 'CategoryB', ...]
根据这一点,用Python打印此信息的方法是:
category_ul = ""
current_depth = -1
category_list.pop(0)
while category_list:
current_node = category_list.pop(0)
category_name = current_node['name']
category_depth = current_node['depth']
if category_depth > current_depth:
category_ul += "<ul>\n"
if category_depth < current_depth:
category_ul += "</ul>\n" * (current_depth - category_depth)
category_ul += "<li>%s</li>\n" % str(category_name)
current_depth = category_depth
if not category_list:
category_ul += "</ul>\n" * (current_depth + 1)
但是,当前_深度始终定义为-1。如果从上面的模板代码中看不出这一点,我对它还很陌生,因此非常感谢您的帮助!谢谢
<tal:block tal:define="current_depth -1">
<tal:block tal:repeat="category categories_list">
<ul tal:condition="python:category['depth'] > current_depth">${category['name']}</ul>
<tal:block tal:define:"current_depth category['depth']"></tal:block>
<span>current_depth: ${current_depth}</span>
</tal:block>
</tal:block>