python - 夹层的菜单页

标签 python django content-management-system mezzanine

在 Mezzanine/Django 中,我有几个嵌套页面,但只有最深嵌套的页面有内容。对于每个中间页面,我只想显示其直接子页面的菜单。例如,给定布局:

Chapter 1
    Section 1.1
        Subsection 1.1.1
        Subsection 1.1.2
    Section 1.2
        Subsection 1.2.1
Chapter 2
    Section 2.1
        Subsection 2.1.1

标记为chapter-1的页面应该只显示以下内容的列表

* Section 1.1
* Section 1.2

我可以通过为每个章节创建自定义模板来实现此目的,其中包含:

{% load pages_tags %}
{% page_menu "pages/menus/chapter_menu.html" %}

其中 chapter_menu.html 是我的菜单模板:

{% load pages_tags %}
<ul>
{% for page in page_branch %}
{% if page.in_menu %}
{% if page.is_current_child %}
<li>
    <a href="{{ page.get_absolute_url }}">{{ page.title }}</a>
</li>
{% else %}
{% page_menu page %}
{% endif %}
{% endif %}
{% endfor %}
</ul>

但是,每个模板名称(例如 pages/chapter-1.html)必须与其引用的页面的 slug 匹配,因此我需要为每个章节和每个部分复制这些模板.

在不重复模板的情况下执行此操作的正确方法是什么?

最佳答案

我认为您不需要为每个菜单创建一个模板。我通过使用enter link description here中的tree.html和my_menu.html解决了这个问题

所以我只是将 menus/tree.html 替换为:

{% load i18n future pages_tags %}
{% spaceless %}
{% page_menu "pages/menus/my_menu.html" %}
{% endspaceless %}

并添加了menu/my_menu.html:

{% load pages_tags %}
{% for page in page_branch %}
    {% if page.is_current_child %}
    <a href="{{ page.get_absolute_url }}">{{ page.title }}</a>
    {% endif %}
    {% if page.is_current_or_ascendant %}
        {% if page.has_children_in_menu %}{% page_menu page %}{% endif %}
    {% endif %}
{% endfor %}

关于python - 夹层的菜单页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29823990/

相关文章:

django - Azure 应用服务托管的 Django APP 的管理面板中没有 CSS

python - 如何在 django admin 中显示我的模型的所有字段?

asp.net-mvc - MVC 4 限制所有操作/ Controller ,除非管理员或角色/用户允许

python - 覆盖 os.walk 以返回生成器对象作为第三项

python - 在给定的可迭代对象上滑动宽度为 n 的窗口

asp.net - 如何使用python+mechanize在asp.net网站上按下按钮

php - 发布 PHP CMS ......如何?

python - 获取函数的用户输入

javascript - Django Admin 弹出功能

ruby-on-rails - 谁能推荐一个好的轻量级 Rails CMS?