DjangoCMS / Django - detecting if current page is child - django

I'm looking to customise the sub_menu.html template with DjangoCMS, and have currently got the following code for the menu:
{% if children %}
<div class="unit subnav">
<h3>{% page_attribute "menu_title" %}</h3>
<ul>
{% for child in children %}
<li class="{% if child.selected %}on{% endif %}{% if child.ancestor %}ancestor{% endif %}{% if child.sibling %}sibling{% endif %}{% if child.descendant %}descendant{% endif %}">
{{ child.get_menu_title }}
</li>
{% endfor %}
</ul>
</div>
{% endif %}
So basically, this detects if the page has children, and then adds the sub navigation if children of this page exist.
So far, so good.
My problem is that when I navigate to the child pages themselves - the menu disappears, so I'd like to detect if the page is a "child" below level 1. This should stop all pages having a nav (because they're all children of the homepage I presume) but should allow those below the main level of navigation to have the menu appear.
If anybody can lend a hand or point me in the right direction, that would be brilliant.

OK, it might not make sense to people, but I was able to get to the bottom of this with the following if statement within my subnav template...
{% if children or request.current_page.level > 0 %} subnav in here {% endif %}

Related

Developing django-cms dropdown menus

I'm new to django and django-cms. I read the official docs and tried to find any other info on the internet, that could guide me through making dropdown menus in django cms. But unfortunately for me, the official docs are really raw (I don't get it at all what's about the menus) and on the internet there's nothing sensible enough.
Please explain me or give a step by step guide on this topic.
At this moment the menu is working and the only code that I have for that is a pair of <ul> and this {% show_menu 0 100 100 100 %} inside them.
The most simple way to get dropdowns working in django-cms is to follow this:
(In this example I am using Bootstrap code for my menu)
In your base.html file (or any other name that you are using) use this way of showing your menu {% show_menu 0 10 10 10 %}
As an example, this is what i used in my code in base.html to render the menu:
<ul>
{% show_menu 0 10 10 10 "menu/custom-menu.html" %}
</ul>
Now create the template in the directory "/templates/menu/custom-menu.html" with these sekizai tags and similar html code that will overwrite your base.html and render your menu with drop-down elements:
<div class="dropdown">
{% for child in children %}
<!-- no child pages -->
{% if child.is_leaf_node %}
<li>{{child.get_menu_title }}</li>
{% endif %}
<!-- /no child pages -->
<!-- has child pages -->
{% if not child.is_leaf_node or child.ancestor %}
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
{{child.get_menu_title }}<b class="caret"></b></a>
<ul class="dropdown-menu">
{% if child.get_descendants %}
{% for kid in child.get_descendants %}
<li>
<a href="{{ kid.get_absolute_url }}">
{{kid.get_menu_title }}
</a>
</li>
{% endfor %}
{% endif %}
</ul>
</li>
{% endif %}
<!-- /has child pages -->
{% endfor %}
<!-- /end for child -->
</div>
And dont forget to put the {% load menu_tags %} sekizai tag in the top of your custom-menu.html file.

How to Create a Mezzanine menu including children elements of current page

I want to create a Menu that handles information about the current page and their siblings for example, if I have the following tree:
If I am in page Child 1 my menu should only display Page 1 and the children 1-3
This can be achieved with the following code:
{{ page.parent.title }}
<ul>
{% page_menu page.parent %}
</ul>
The problem I have is that I would like to display the Grand Child 1 and 2 if I am located in Child 2. How can I do that?
I tried the following:
<ul>
{% for page in branch_page %}
{% if page.is_current_sibling %}
<li> {{ page.title }}
{% if page.is_current_or_ascendant %}
{% page_menu page %}
{% endif %}
{% endif %}
{% endfor %}
</ul>
But it doesn't work and it does not display any error, how can I do this in another way?
The page_menu template tag lets you pass in an optional parent page, and a menu template that will be used recursively for rendering children, eg where "page" is the current page:
{% load pages_tags %}
{% page_menu page "pages/menus/tree.html" %}
ref: https://groups.google.com/forum/#!msg/mezzanine-users/Fk2Nm6K6qho/VyePF5D6CXgJ

Make menu item not clickable in Django CMS

I have a Django application running using Django CMS.
I am trying to make one specific submenu not clickable.
The menu is like this:
item1|item2|item3
sub_item3
sub_item3
What I want is that everything is clickable except for "item3" menu item.
How can I achieve this knowing item3 is a Django CMS page itself as are each of its children pages?
The idea behind this is to prevent that the user sees an empty page when he clicks on the top "item3" menuitem.
Here is how I have done this to keep it from linking anywhere:
{% load cms_tags menu_tags %}
{% for child in children %}
<li>
{% if child.is_leaf_node %}{{ child.get_menu_title }}{% else %}{{ child.get_menu_title }}{% endif %}
{% if child.children %}
<ul>
{% show_menu 0 100 100 100 "menu/top-menu.html" "" "" child %}
</ul>
{% endif %}
</li>
{% endfor %}
In your case you may want a class with some css like this from Disable a link using css
.active {
pointer-events: none;
cursor: default;
}
Ok I fixed it with a mix of the previous answer and these links https://groups.google.com/forum/?fromgroups=#!topic/django-cms/aS2Ew2mf8XY and the docs https://django-cms.readthedocs.org/en/2.4.0/extending_cms/app_integration.html#how-it-works (navigation modifiers).
from menus.base import Modifier
from menus.menu_pool import menu_pool
class MyMode(Modifier):
"""
"""
def modify(self, request, nodes, namespace, root_id, post_cut, breadcrumb):
if post_cut:
return nodes
for node in nodes:
if node.title == u'Page not clickable':
node.no_click = True
return nodes
menu_pool.register_modifier(MyMode)
With this template
% load cms_tags menu_tags %}
{% for child in children %}
<li>
{% if not child.no_click %}{{ child.get_menu_title }}{% else %}{{ child.get_menu_title }}{% endif %}
{% if child.children %}
<ul>
{% show_menu from_level to_level extra_inactive extra_active template "" "" child %}
</ul>
{% endif %}
</li>
{% endfor %}
And in my main template (base.html) I add the menu with
{% show_menu 0 100 100 100 "menu.html" %}

Navigation Nodes in django-cms example

Can you give me an example how can i use Navigation Nodes?
Cannot find examples in documentation.
There is this {{ node }} but where is it coming from?
Particalarly i am intereisted in {{ node.is_leaf_node }}.
Each navigation node is simply a link/entry in your menu tree so they are generated from your page layout, for example:
- Home
- About
- Projects
- Project A
- Project B
- Contact
creates a menu with each page representing a node in the menu tree.
There's an example of them working in the default menu.html template (where child is a node in the menu):
{% load menu_tags %}
{% for child in children %}
<li class="{% if child.selected %}selected{% endif %}{% if child.ancestor %}ancestor{% endif %}{% if child.sibling %}sibling{% endif %}{% if child.descendant %}descendant{% endif %}">
{{ child.get_menu_title }}
{% if child.children %}
<ul>
{% show_menu from_level to_level extra_inactive extra_active template "" "" child %}
</ul>
{% endif %}
</li>
{% endfor %}

How to add a class to a menu item that has children (nested pages) in Django-CMS

I'm working on a project where there's some specific styling on a menu item which has nested children. The menu structure looks something like this
Home
|
About
|
Services
|_ web design
|_ social marketing
|_ traditional marketing
I'm using {% show_menu 0 100 100 100 "menu.html" %} in my template and I have the following inside my menu.html:
{% load menu_tags %}
{% for child in children %}
<li class="{% if child.selected %}active{% endif %}{% if child.sibling %}dropdown{% endif %}">
{{ child.get_menu_title }}
{% if child.children %}
<ul>
{% show_menu from_level to_level extra_inactive extra_active template "" "" child %}
</ul>
{% endif %}
</li>
{% endfor %}
I put {% if child.sibling %}dropdown{% endif %} in there to illustrate where I want the class to be added, but targeting it as a child.sibling is not the right way of doing it. Is there a way to target the specific dropdown like this {% if child.has_children %}dropdown{% endif %}?
Thanks
{% if child.children %}...{% endif %}