Django CMS:基于所选插件的页面导航菜单
我在django cms中有一个模板,它基于我添加到内容占位符的插件创建了一个视差网站。这是我的模板:Django CMS:基于所选插件的页面导航菜单,django,django-cms,Django,Django Cms,我在django cms中有一个模板,它基于我添加到内容占位符的插件创建了一个视差网站。这是我的模板: {% extends "foundry/base.html" %} {% load cms_tags %} {% block title %}{% page_attribute "page_title" %}{% endblock title %} {% block content %} {% placeholder 'content' %} {% endblock content
{% 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 01 100 100“foundry/menu.html”%}生成菜单。我想根据我添加到内容占位符中的插件将项目添加到此菜单。因为在cms渲染占位符之前调用了show_菜单,所以我无法使用NavigationNode注册我的菜单。如果我可以查询内容占位符中使用的插件,我就可以处理这个菜单。但Django CMS数据库太复杂了,我找不到查询。
谢谢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())
拥有占位符插件后,您可以通过以下方式在菜单上进行所需的任何自定义:
希望这对您有所帮助。如果您仍有兴趣这样做,下面是我在菜单中插入锚插件的实现:
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文件中