Django CMS多级下拉菜单

7
我有点新手在Django CMS中,尽力避免询问,但这个问题让我发疯。 我创建了一个带有主题和类别模型的Wiki应用程序。我将其连接到我的CMS上的网站并将其添加到我的菜单中。现在我想能够在我的菜单上显示所有顶级类别、它们的子类别和主题,以及这些子类别的子类别等。
Menu/Navigation should look like this:

Wiki
    Category1
        Category1.1
            Topic
        Category1.2
        Topic
    Category2
        Topic
    Category3
        ...

Right now i can only show the Top categories:

Wiki
    Category1
    Category2
    Category3

我已经创建了一个menu.py文件,在我的Wiki页面上获得了一个自定义子菜单(就是您在上面看到的那个):

menu.py

class WikiSubMenu(CMSAttachMenu):
    name = _("Wiki Sub-Menu")

    def get_nodes(self, request):
        nodes = []
        categories = Category.objects.filter(parent_id__isnull=True)

        for c in categories:
            node = NavigationNode(
                mark_safe(c.name),
                c.get_absolute_url(),
                c.id,

            )

            nodes.append(node)

        return nodes

menu_pool.register_menu(WikiSubMenu)

我的分类模型:
class Category(models.Model):
    ''' Category model. '''
    name = models.CharField(max_length=100)
    slug = models.SlugField(unique=True)
    description = models.TextField(blank=True)
    parent = models.ForeignKey(
        'self',
        null=True,
        blank=True,
        related_name='children'
    )
    sort = models.IntegerField(default=0)

    class Meta:
        ordering = ['sort', 'name']

    def __unicode__(self):
        return self.name

    @models.permalink
    def get_absolute_url(self):
        return ('topics:categories_category_detail', (), {'slug': self.slug})

    def get_all_children(self):
        return Category.objects.filter(parent=self)

现在,是否可以为所有具有子级别的类别创建一个子子菜单,以及它们的子级别,以及它们的子级别等等?
感谢您的帮助,对糟糕的英语表示歉意。
--编辑:--
我刚刚发现了这个:

docs.django-cms.org/en/3.0.6/extending_cms/app_integration.html#integration-modifiers

我认为这就是我要找的,我有点盲目,没有找到它。我会尝试并发布答案,如果它起作用了。

-- 编辑(再次):--

修改器对我没有起作用,但我进一步了解了文档,发现我可以给NavigationNodes一个可选的属性字典,其中填充了所有父级为c的类别,这样我就得到了所需的数据,然后我发现了非常好用的bootstrap下拉菜单,它正好做我想要的事情。因此,我的代码现在看起来像这样:

menu.py

class TopicsSubMenu(CMSAttachMenu):
    name = _("Wiki Sub-Menu")

    def get_nodes(self, request):
        nodes = []
        categories = Category.objects.filter(parent_id__isnull=True)

        for c in categories:
            node = NavigationNode(
                mark_safe(c.name),
                c.get_absolute_url(),
                c.pk,
                attr=dict(
                    subcategories=Category.objects.filter(parent=c),),
            )

            nodes.append(node)
        return nodes

我的模板:

menu.html

{% for child in children %}
    <li>
        {% if child.children %}

            <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                {{ child.get_menu_title }}
                <span class="caret">
                </span>
            </a>
            <ul class="dropdown-menu multi-level" role="menu" aria-labelledby="dropdownMenu">
                {% for child in child.children %}
                    {% if child.attr.subcategories.count %}
                        <li class="dropdown-submenu">
                            <a tabindex="-1" href="#">{{ child.get_menu_title }}</a>
                            <ul class="dropdown-menu">
                                {% for subcategory in child.attr.subcategories %}
                                <li>
                                    <a tabindex="-1" href="{{ subcategory.get_absolute_url }}">{{ subcategory }}</a>
                                </li>
                                {% endfor %}
                            </ul>


                        </li>
                    {% else %}
                    <li><a href="{{child.get_absolute_url}}">{{ child.get_menu_title }}</li></a>
                    {% endif %}
                {% endfor %}

            </ul>
        {% else %}
            <a href="{{ child.get_absolute_url }}">
                <span>
                    {{ child.get_menu_title }}
                </span>
            </a>
        {% endif %}
    </li>

    {% if class and forloop.last and not forloop.parentloop %}
    {% endif %}


{% endfor %}

我的下一步是将整个“for”循环从模板中编写成一个方法,使用while循环或其他方式进行递归,并将结果发布为答案。
我希望我能通过这些东西帮助某人 :)
1个回答

10

哇哦!我终于做到了!

base.html

<div class="navbar-collapse collapse">
    <ul class="nav navbar-nav">
        {% show_menu 0 100 100 100 "menu.html" %}
    </ul>
</div>

menu.html:

{% for child in children %}
    <li class="child{% if child.selected %} selected{% endif %}{% if child.ancestor %} ancestor{% endif %}{% if child.sibling %} sibling{% endif %}{% if child.descendant %} descendant{% endif %}{% if child.children %} dropdown{% endif %}">

        <a {% if child.children %}class="dropdown-toggle" data-toggle="dropdown"{% endif %} href="{{ child.attr.redirect_url|default:child.get_absolute_url }}">
            <span>{{ child.get_menu_title }}</span>{% if child.children|length %}<span class="caret"></span>{% endif %}
        </a>

        {% if child.children %}
            <ul class="dropdown-menu multi-level" role="menu" aria-labelledby="dropdownMenu">
                {% show_menu from_level to_level extra_inactive extra_active "dropdownmenu.html" "" "" child %}
            </ul>
        {% endif %}

    </li>
    {% if class and forloop.last and not forloop.parentloop %}{% endif %}

{% endfor %}

以下是我的dropdownmenu.html文件: (递归部分从这里开始)

{% load i18n menu_tags cache mptt_tags %}
{% for child in children %}
    <li {% if child.children %}class="dropdown-submenu"{% else %} {% endif %}>
        <a tabindex="-1" href="{{ child.attr.redirect_url|default:child.get_absolute_url }}">{{ child.get_menu_title }}</a>
        {% if child.children %}
            <ul class="dropdown-menu">
                {% show_menu from_level to_level extra_inactive extra_active "dropdownmenu.html" "" "" child %}
            </ul>
        {% endif %}

    </li>
{% endfor %}

而且最重要的是,menu.py:

class TopicsSubMenu(CMSAttachMenu):
    name = _("Wiki Sub-Menu")

    def get_nodes(self, request):
        nodes = []
        categories = Category.objects.all()

        for c in categories:
            node = NavigationNode(
                mark_safe(c.name),
                c.get_absolute_url(),
                c.pk
            )
            if c.parent:
                node.parent_id = c.parent_id
            nodes.append(node)

        topics = Topic.objects.all().exclude(category__isnull=True)
        for t in topics:
            node = NavigationNode(
                mark_safe(t.title),
                t.get_absolute_url(),
                t.pk,
                t.category.all()[0].id,
                parent_namespace="TopicsSubMenu"
            )
            nodes.append(node)
        return nodes

menu_pool.register_menu(TopicsSubMenu)

这就是全部内容了!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接