How to show a certain number of levels in django-mptt? - python

In my Django project I use django-mptt application to create hierarchical tree. Right now next code works well but I want to show only first 4 level of the tree. How to make it correctly? I am confused.
views.py:
context['caregories'] = Category.objects.get(id=5).get_descendants()
html:
{% load mptt_tags %}
<ul>
{% recursetree caregories %}
<li>
{{ node.name }}
{% if not node.is_leaf_node %}
<ul class="children">
{{ children }}
</ul>
{% endif %}
</li>
{% endrecursetree %}
</ul>

You can filter the descendants by their level
obj = Category.objects.get(id=5)
context['caregories'] = obj.get_descendants().filter(level__lte=obj.level + max_depth)
where max_depth is the depth you require

Related

Django mptt recursetree with get_cached_trees in template

In View:
context['categories'] = = models.Category.objects.all().get_cached_trees()
In template:
{% load mptt_tags %}
<ul>
{% recursetree categories %}
<li>
{{ node.name }}
{% if not node.is_leaf_node %}
<ul class="children">
{{ children }}
</ul>
{% endif %}
</li>
{% endrecursetree %}
</ul>
As a result, it renders only first level of queryset. If remove get_cached_trees it renders all tree. How to render all tree with get_cached_trees?
You don't actually need to call get_cached_trees() in this situation because recursetree already does the caching for you.
From the documentation:
Iterates over the nodes in the tree, and renders the contained block for each node.
This tag will recursively render children into the template variable {{ children }}.
Only one database query is required (children are cached for the whole tree)

get_children not working. get_descendants does. But i can't use that

I am currently working on the navbar of a project with Django-cms. I am fairly new to this framework and language, so sorry if this is a stupid question.
This has double dropdowns, which respond to user changes in the Django-cms admin interface.
Which works. Sort of.
The problem is that get_children doesn't work (no errors or something, just not detecting children, and showing the 'should be dropdown button' as a non dropdown version), but get_descendants does. But if i use that the content of the second dropdown will be shown again in the first dropdown. So get_children will be perfect, as it will only show the direct descendants, instead of all.
{% load cms_tags menu_tags sekizai_tags staticfiles%}
{% load menu_tags %}
{% for child in children %}
<!--non dropdown 1-->
{% if child.is_leaf_node %}
<li>{{child.get_menu_title }}</li>
{% endif %}
<!--dropdown 1-->
{% if not child.is_leaf_node or child.ancestor %}
<div class="dropdown">
<li>{{child.get_menu_title }}<b class="caret"></b></li>
<!-- dropdown 1 content-->
{% if child.get_descendants %}
<div class="dropdown-content">
{% for kid in child.get_descendants %}
<!--non dropdown 2-->
{% if kid.is_leaf_node %}
<li>{{kid.get_menu_title }}</li>
{% endif %}
<!--dropdown 2 -->
{% if not child.is_leaf_node or child.ancestor %}
<li>
<a class="menu-has-sub">{{kid.get_menu_title }}<i class="fa fa-angle-down"></i></a>
<!-- dropdown 2 content-->
<ul class="sub-dropdown droppeddown">
{% for baby in kid.get_descendants %}
<li>{{baby.get_menu_title }}</li>
{% endfor %}
</ul>
</li>
{% endif %}
{% endfor %}
</div>
{% endif %}
</div>
{% endif %}
{% endfor %}
So my question is: Why can't i use children
EDIT: *Why can't i use get_children. As in the function. No child labour here.
Nvm i fixed it! The syntax in this case should be children instead of get_children. which is funny because of that edit above.
Anyway, here's an example:
This doesn't work:
{% for kid in child.get_children %}
This does work:
{% for kid in child.children%}
hopefully this will help anyone else having this little struggle.

How can you create multiple menus with django-cms

I'm trying to have two separate menus in my django-cms app. One for the header and another with a different set of links for the footer:
[ Logo ] Link_A Link_B Link_C Link_D
... content ...
Link_E Link_F Link_G Link_H
Using baked in {% show_menu %}, will show all of the pages registered, links A - H, and doesn't allow me to separate the two menus.
How can I create two separate menus?
Depends what you want to do really, but I've got a base template which has a navigation menu at the top and a sitemap submenu at the bottom.
So starting with the navigation;
{% show_menu 1 100 100 100 "partials/navigation.html" %}
Which uses the template;
{% load cms_tags menu_tags cache cms_page %}
{% for child in children %}
<li>
<a href="{{ child.attr.redirect_url|default:child.get_absolute_url }}">
{{ child.get_menu_title }}
</a>
{% if child.children and child.level <= 4 %}
<ul>
{% show_menu from_level to_level extra_inactive extra_active template '' '' child %}
</ul>
{% endif %}
</li>
{% endfor %}
Then the sitemap;
{% show_sub_menu 2 1 1 "partials/sitemap.html" %}
And sitemap.html
{% load cms_tags cms_page cache %}
{% if children %}
{% for child in children %}
<ul class="site-footer__column">
<li>
<h4>
<a href="{{ child.attr.redirect_url|default:child.get_absolute_url }}">
{{ child.get_menu_title }}
</a>
</h4>
</li>
{% if child.children %}
{% for baby in child.children %}
<li class="footer_sub">
<a href="{{ baby.attr.redirect_url|default:baby.get_absolute_url }}">
{{ baby.get_menu_title }}
</a>
</li>
{% endfor %}
{% endif %}
</ul>
{% endfor %}
{% endif %}
Understanding the options (numbers) you can provide for a menu can enable you to display different parts of your site, but if the built in menu tags don't suit your needs, you could write a custom menu tag.
The standard menu docs are here; http://docs.django-cms.org/en/3.2.2/reference/navigation.html
And here are the docs for customising your menus; http://docs.django-cms.org/en/3.2.2/how_to/menus.html

Django-CMS node tags in breadcrumbs template

I want to create custom breadcrumbs template for my site developed with django-cms.
It should display levels (number) of items in addition to item title. I've read this article and it seems that I should use {{ node.level }} like this, for example:
{% for ance in ancestors %}
<li>
{% if not forloop.last %}
**{{ ance.level }}** - {{ ance.get_menu_title }} <span class="separator">ยป</span>
{% else %}
<span class="active">**{{ ance.level }}** -{{ ance.get_menu_title }}</span>
{% endif %}
</li>
{% endfor %}
But this is didn't working. I exepected to see "0 - Main ..." but see only " - Main ..."
And I am also curious about get_menu_title because I didn't find that in documentation, but I am find {{ node.title }} and it seems working same as get_menu_title

Django-cms show menu: How to show menu under current page?

I'm totally confused by django-cms's show_menu tag. There are four parameters but no full document on these parameters could be found. There are only several exmaples however I cannot find how to show menu under current page only.
Pages are arranged like this:
--Projects
----proj1
----proj2
--Gallery
----gal1
----gal2
In Projects template, how do I set the parameters for show_menu to show only the menu under current page?
Update
#Brandon
I tried exactly this:
{% show_sub_menu 1 "menu/cust_menu.html" %}
As exactly what the document says. However it ends up in this error:
u'menu/cust_menu.html' could not be converted to Integer
You need to use:
{% show_sub_menu 1 %}
http://django-cms.readthedocs.org/en/2.1.3/advanced/templatetags.html#show-sub-menu
There is actually an error in documentation and it seems to be also a little bug introduced in one of the last versions of django cms (planned to be solved in django-cms 3.0 version!).
https://github.com/divio/django-cms/issues/1913
I solved using this:
{% show_menu_below_id "topics_page" 0 4 100 100 "./_menus/menu_topics.html" %}
where "topics_page" is the reverse id (you configure it in advanced section in cms admin).
For recursive rendering of menu, just configure the custom id of subpages for which you want display the next menu level;
in your custom menu template, you can play with child properties and the for loop counter. Below, check a nasty example but still useful if you want to customize your menu template:
{% load menu_tags %}
{% load template_extras %}
{% for child in children %}
{# sub voices topics #}
{% if child.level == 1 %}
{% if not forloop.counter|divisibleby:2 %}
<div class="row-fluid">
{% endif %}
<div class="span6">
<div class="sub1">
<a href="{{ child.attr.redirect_url|default:child.get_absolute_url }}"><span
class="icon-play"></span>{{ child.get_menu_title|capfirst }}</a>
</div>
{% if child.children %}
{% show_menu_below_id child.attr.reverse_id 0 4 100 100 template %}
{% endif %}
</div>
{% if forloop.counter|divisibleby:2 %}
</div> <!-- end row fluid -->
{% endif %}
{% elif child.level == 2 %}
{# 2 - {{ child.attr.reverse_id}} - {{ child.get_menu_title }}#}
<div class="row-fluid">
<div class="span11 offset1">
<div class="sub2">
<a href="{{ child.attr.redirect_url|default:child.get_absolute_url }}">{{ child.get_menu_title|capfirst }}
</a></div>
</div>
</div>
{% if child.children %}
{% show_menu_below_id child.attr.reverse_id 0 4 100 100 template %}
{% endif %}
{% elif child.level == 3 %}
{# leaf node topics #}
{# 3 - {{ child.attr.reverse_id}} - {{ child.get_menu_title }}#}
<div class="row-fluid">
<div class="span10 offset2">
<div class="sub3"><a href="{{ child.attr.redirect_url|default:child.get_absolute_url }}">
<i class="icon-list-alt"></i> {{ child.get_menu_title|capfirst }}</a></div>
</div>
</div>
{% endif %}
{% endfor %}

Categories

Resources