首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在模板中包含get_cached_trees的Django mptt递归树

在模板中包含get_cached_trees的Django mptt递归树
EN

Stack Overflow用户
提问于 2020-02-19 04:21:23
回答 1查看 407关注 0票数 0

在视图中:

代码语言:javascript
复制
context['categories'] =  = models.Category.objects.all().get_cached_trees()

在模板中:

代码语言:javascript
复制
{% load mptt_tags %}
<ul>
    {% recursetree categories %}
        <li>
            {{ node.name }}
            {% if not node.is_leaf_node %}
                <ul class="children">
                    {{ children }}
                </ul>
            {% endif %}
        </li>
    {% endrecursetree %}
</ul>

因此,它只呈现第一级查询集。如果移除get_cached_trees,它将呈现所有树。如何用get_cached_trees渲染所有的树

EN

回答 1

Stack Overflow用户

发布于 2020-02-19 12:49:09

在这种情况下,您实际上不需要调用get_cached_trees(),因为递归树已经为您执行了缓存。

从文档中:

代码语言:javascript
复制
Iterates over the nodes in the tree, and renders the contained block for each node.
This tag will recursively render children into the template variable {{ children }}.
Only one database query is required (children are cached for the whole tree)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60288862

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档