我在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”%}来生成菜单。我想根据我添加到内容占位符的插件将项目添加到此菜单。因为show_menu在cms渲染占位符之前调用,所以我不能使用NavigationNode来注册我的菜单。如果我可以查询内容占位符中使用的插件,我可以处理此菜单。但Django CMS数据库是如此复杂,我找不到查询。 感谢
答案 0 :(得分:1)
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
希望能帮到你。
答案 1 :(得分:0)
如果你仍然对此感兴趣,这里是我的实现,它将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文件中。