如何知道Django-CMS中当前页面的级别

时间:2016-10-18 18:39:52

标签: django django-cms

我的page.html有这个

{% extends "base.html" %}
{% load cms_tags menu_tags %}

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

{% block content %}
  {% placeholder "content" %}
  {% show_menu 2 0 0 100 %}
{% endblock content %}

但依赖于当前页面的级别,我必须在页面上显示不同的元素,类似这样的

{% if node.menu_level == 2 %}
  {% show_menu 2 0 0 100 %}
{% else %}
  #do something different
{% endif %}

怎么做?如何知道当前页面的级别?

1 个答案:

答案 0 :(得分:1)

我这样做是为了根据页面的祖先选择相关的页面标题;

{% if request.current_page.get_ancestors|length <= 1 %}
    <h1 class="pipe-title pipe-title--inset">
        {{ request.current_page.get_page_title }}
    </h1>
{% else %}
    {% for ance in request.current_page.get_ancestors %}
        {% if ance.depth == 2 %}
            <h1 class="pipe-title pipe-title--inset">{{ ance.get_page_title }}</h1>
        {% endif %}
    {% endfor %}
{% endif %}

所以你可以根据菜单树中的页面深度做任何你想做的事。

相关问题