【问题标题】:Django CMS menu, how to set a class to parents only?Django CMS菜单,如何只为父母设置课程?
【发布时间】:2015-01-29 16:55:00
【问题描述】:

我的菜单需要改进。我需要在父菜单项上有一个类,但是通过我的递归,它不起作用。

这是(部分)我的菜单:

Home
    Teachers (id teachers)
        Contact
        Info
        Projects
          myproject
          yourproject

我从这样的“老师”开始:

{% show_menu_below_id "teqchers" 0 1 0 1 "teachers_menu.html" %}

这是我的 Teachers_menu.html:

{% load menu_tags %}
{% for child in children %} 
    <li class="{% if child.selected %}selected parent_{{forloop.counter}}{% endif %} {% if child.sibling %}parent_{{forloop.counter}} {% endif %}"> 
        <a href="{{ child.get_absolute_url }}">{{ child.get_menu_title }}</a>
        {% if child.children %}
        <div class="submenu">   
            <ul>
                {% show_menu from_level to_level extra_inactive extra_active template "" "" child %}
            </ul>
        </div>
        {% endif %}
    </li>
{% endfor %}

有了这个,我的菜单就可以工作了。

当我单击“项目”时,一切正常,两个项目都在视图中。但是当我点击一个项目时,我希望页面显示,但它没有,它确实重建了我的菜单,并将所需的类添加到子元素:

{% if child.selected %}selected parent_{{forloop.counter}}{% endif %}

很明显,因为我猜它现在是一个孩子,但是如何防止这种情况呢?对于第一个菜单项,我只需要该类。

【问题讨论】:

    标签: css django menu django-cms


    【解决方案1】:

    不要这样做:

    {% show_menu from_level to_level extra_inactive extra_active template "" "" child %}
    

    我现在添加了另一个这样的模板:

    {% show_menu from_level to_level extra_inactive extra_active "teachers_submenu.html" "" "" child %}
    

    在那个模板中我现在有了:

    {% load menu_tags %}
    {% for child in children %} 
        <li class="{% if child.selected %}selected{% endif %}"> 
            <a href="{{ child.get_absolute_url }}">{{ child.get_menu_title }}</a>
        </li>
    {% endfor %}
    

    所以额外的模板负责子菜单。通过一些样式,它现在可以工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-30
      • 1970-01-01
      • 2015-03-28
      • 1970-01-01
      • 1970-01-01
      • 2019-05-06
      • 2016-01-26
      • 2016-07-13
      相关资源
      最近更新 更多