【问题标题】:Django CMS: In-Page Navigation Menu based on selected pluginsDjango CMS:基于所选插件的页面内导航菜单
【发布时间】:2016-03-19 17:24:59
【问题描述】:

我在 django cms 中有一个模板,它根据我添加到内容占位符的插件创建了一个视差网站。这是我的模板:

{% extends "foundry/base.html" %}
{% load cms_tags %}

{% block title %}{% page_attribute "page_title" %}{% endblock title %}

{% block content %}
    {% placeholder 'content' %}
{% endblock content %}

在 base.html 中,我使用 {% show_menu 0 1 100 100 "foundry/menu.html" %} 来生成菜单。我想根据添加到内容占位符的插件向此菜单添加项目。因为在 cms 渲染占位符之前调用了 show_menu,所以我不能使用 NavigationNode 来注册我的菜单。如果我可以查询内容占位符中使用的插件,我可以处理这个菜单。但是 Django CMS 数据库太复杂了,我找不到查询。 谢谢

【问题讨论】:

    标签: django django-cms


    【解决方案1】:

    以防万一您仍然对此感兴趣,这是我将 Anchor 插件插入菜单的实现:

    from menus.base import Modifier, NavigationNode
    from menus.menu_pool import menu_pool
    from cms.models import Page
    from cms.utils.plugins import get_plugins
    from cms.templatetags.cms_tags import _get_placeholder
    
    class AnchorPluginMenuModifier(Modifier):
        """
    
        """
        def modify(self, request, nodes, namespace, root_id, post_cut, breadcrumb):
            # if the menu is not yet cut, don't do anything
            if post_cut:
                return nodes
            # otherwise loop over the nodes        
            newnodes = []
            for node in nodes:
                try: 
                    if "is_page" in node.attr and node.attr["is_page"]:
    
                        page_obj = Page.objects.get(id=node.id)                    
                        template_context = {
                            "request" : request,
                        }
                        placeholder_name = "content"
    
                        placeholder = _get_placeholder(request.current_page, page_obj,
                            template_context, placeholder_name)  # placeholder_name is a string
                        plugins = get_plugins(request, placeholder, page_obj.get_template())
    
    
                        for plugin in plugins:
                            if type(plugin).__name__ == "AnchorPluginModel":
                                newnode = NavigationNode(
                                    plugin.anchor_menutitle,
                                    node.url+"#"+plugin.anchor_name,
                                    "{}-{}".format(page_obj.id,plugin.id),
                                    node
                                )
                                newnode.parent_id = node.id
                                newnodes.append(newnode)
                                setattr(newnode, "selected", False)
                                node.children.append(newnode)                                       
                except Exception, e:
                    print e
                    # client.captureException()
    
            return nodes
    
    menu_pool.register_modifier(AnchorPluginMenuModifier)
    

    代码位于 cms_menus.py 文件中。

    【讨论】:

      【解决方案2】:

      Django CMS 提供了几个实用程序来执行此操作;您只需挖掘源代码即可找到它们。

      from cms.templatetags.cms_tags import _get_placeholder
      from cms.utils.plugins import get_plugins
      
      if request and request.current_page:
          placeholder = _get_placeholder(request.current_page, request.current_page,
              template_context, placeholder_name)  # placeholder_name is a string
          plugins = get_plugins(request, placeholder, request.current_page.get_template())
      

      获得占位符插件后,您可以通过以下方式在菜单上进行任何自定义:http://docs.django-cms.org/en/develop/how_to/menus.html

      希望对你有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-02-01
        • 1970-01-01
        • 2013-11-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-06
        • 1970-01-01
        相关资源
        最近更新 更多