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 %}
Related
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.
I got a navbar that will have four links,
{% block navbar %}
<div id="navbar">
Link 1
Link 2
Link 3
Link 4
</div>
{% endblock %}
A class='current' will define the current active page like
<a href="" class='current'>Link 4</a>
My question, is overriding the block navbar on each of the 4 links the best way to do it in Django template language so that each link will have a corresponding class 'current'?
There are many ways to solve this, the right one depends on your exact project. This being said, the simplest solution that could possibly work is to use the {% with %} tag and {{ block.super }}, ie:
In your base template:
{% block navbar %}
<div id="navbar">
Link 1
Link 2
Link 3
Link 4
</div>
{% endblock %}
And then in the template for the "Link1" page:
{% block navbar %}
{% with current="link1" %}
{{ block.super }}
{% endwith %}
{% endblock %}
etc...
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
The model gigi has got a typology attribute, I want to add items into a html div tag based on the value of the typology attribute. How can I manage to do this in Djangos templates:
for gigi in object_list
<div id="typology1">
{%if gigi.typology == "typology1"> %}
<div> {{gigi.name}}</div>
{% endif %}
</div>
<div id="typology2">
{%if gigi.typology == "typology2"> %}
<div> {{gigi.name}}</div>
{% endif %}
</div>
Is this the only way? are there better DRY (dont repeat yourself) ways?
There is nothing wrong with the code you currently have; though, you could make the following small adjustment and get rid of quite a bit of code.
Why not use the gigi.typology string as the id?
{% for gigi in object_list %}
<div id="{{ gigi.typology }}">
<div> {{gigi.name}}</div>
</div>
{% endfor %}
If you are looking at having all the elements for a particular "typology" grouped together then you could also use the regroup clause.
{% regoup object_list by typology as typolist %}
{% for typo in typolist %}
<div id="{{ typo.grouper }}">
{% for gigi in typo.list %}
{{ gigi.name }}
{% endfor %}
</div>
{% endfor %}
I am developing an application with python Django
what I need to do is to assign a class to a div based on the counter variable value in templates
{% with 1 as counters %}
{% for importance in all_importance %}
{% if counters == 1 %}
<div class="item active" >
{% else %}
<div class="item" >
{% endif %}
{% for image in importance.subtypemodelimage_set.all %}
<img src="{{ image.image.url }}" />
{% endfor %}
</div>
{% counters += 1 %}
{% endfor %}
{% endwith %}
But I face this issue
Invalid block tag: 'counters', expected 'empty' or 'endfor'
where am I making mistake, thanks in advance for your helps
The for loop sets a number of variables available within the loop (full list in django docs here):
...
forloop.first True if this is the first time through the loop
forloop.last True if this is the last time through the loop
...
you can use forloop.first to check for first loop iteration:
{% for importance in all_importance %}
{% if forloop.first %}
<div class="item active" >
{% else %}
<div class="item" >
{% endif %}
{% for image in importance.subtypemodelimage_set.all %}
<img src="{{ image.image.url }}" />
{% endfor %}
</div>
{% endfor %}
Problem is with
{% counters += 1 %}
There is not tag counters. You are interpreting variable as tag.
More over you can't implement that kind of for loop in django template.