Hi i've been looking all over and can't find the answer to this. I have only 3 months experience in using python/django so excuse my dummy quesion!
Im using django mptt to display a simple nested set navigation.
<ul class="root">
{% recursetree nodes %}
<li>
{{ node.name }}
{% if not node.is_leaf_node %}
<ul class="children">
{{ children }}
</ul>
{% endif %}
</li>
{% endrecursetree %}
this works fine - however i would like to show only children of the selected category (based on slug) and not all of them.
Any ideas ???
i finally did it like this:
{% recursetree nodes %}
<li>
<a href='/{{ node.get_absolute_url}}'>{{ node.name }}</a>
</li>
{% if not node.is_leaf.node %}
{% for c in child %}
{% if c in node.get_children %}
{% if forloop.first %}
<ul class="children">
{{ children }}
</ul>
{% endif %}
{% endif %}
{% endfor %}
{% endif %}
{% endrecursetree %}
in views
category = get_object_or_404(Category, slug=slug)
child = category.get_children()
if not child :
child = category.get_siblings()
but it is a hack. has anyone got better idea?
You need to send down some information about what node you're in, and then it's a simple if statement.
Regarding how to send down the node information universally, there are a couple ways to do this in Django, and none of them are perfect. My preferred method is context processors: http://docs.djangoproject.com/en/1.3/ref/templates/api/#writing-your-own-context-processors
{% recursetree nodes %}
<li>
{{ node.name }}
{% if node.name == category.name %}
<ul>
{{ children }}
</ul>
{% endif %}
<li>
{% endrecursetree %}
You can try this:
{% recursetree nodes %}
#check if the node is a child node
{% if node.is_child_node %}
<a href="{{ node.get_absolute_url }}" >{{ node.name }}</a>
{% endif %}
#check if a root node is the current category
{% if node.is_root_node and node.name == category.name %}
{{ children }}
{% endif %}
{% endrecursetree %}
Related
I am developing a Django application, i had some problem,
I hope to do the following effects on Django,
<div id='cssmenu'>
<ul>
<li class='active'><a href='#'><span>Home</span></a></li>
<li><a href='#'><span>Products</span></a></li>
<li><a href='#'><span>Company</span></a></li>
<li class='last'><a href='#'><span>Contact</span></a></li>
</ul>
</div>
Django Code,
<div id='cssmenu'>
{% for child in children %}
{% cycle 'active' 'last' as cssmenu silent %}
<li class="{{ cssmenu }}">
{{ child.get_menu_title }}
{% if child.children %}
<ul>
{% show_menu from_level to_level extra_inactive extra_active template "" "" child %}
</ul>
{% endif %}
</li>
{% endfor %}
</div>
Could you help me?
That's not what cycle is for: it's for alternating between two or more alternatives. You don't want that at all.
Instead, just use the forloop attributes:
{% for child in children %}
<li class="{% if forloop.first %}active{% elif forloop.last %}last{% endif %}">...</li>
{% endfor %}
Although I suppose you don't want the first one to always be active, but you haven't given any information about how you do want to determine where 'active' goes.
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 %}
Using django-mppt I want to browse my category hierarchy displaying the ammount of objects related to the current category in any of it's children.
Much like drill_down_for_node in the example shown, but only with the current node childrens...
The optimal would be
{% recursetree obj.get_children cumulative count model.Foreignkey.category in o_count%}
<li>
<h2>{{ node }}</h2>
{% if not node.is_leaf_node %}
<ul class="children">
{{ children }} ({{o_count}})
</ul>
{% endif %}
</li>
{% endrecursetree %}
Any pointers?
Found a function for this on the TreeManager:
https://github.com/django-mptt/django-mptt/blob/master/mptt/managers.py#L250
Add the counts to the queryset in your view:
context['qs_with_related_count'] = model1.objects.add_related_count(
obj.get_children(),
model2,
'category',
'o_count',
True
)
and in template:
{% recursetree qs_with_related_count %}
<li>
<h2>{{ node }} ({{ node.o_count }})</h2>
{% if not node.is_leaf_node %}
<ul class="children">
{{ children }}
</ul>
{% endif %}
</li>
{% endrecursetree %}
Unfortunately I was trying to use a ManyToManyField as rel_field:
https://github.com/django-mptt/django-mptt/issues/90
but it looks like you are in luck ('category' not 'categories') :)
I have the following view in my django application
def ViewSale( request ):
salecur = Sale.objects.filter(user=2).order_by('sale_date')
return render_to_response('myapp/sale.html',{'salecur':salecur})
my template looks like this
{% regroup salecur by sale_date as sale_list %}
<ul>
{% for sale_date in sale_list %}
<li>{{ sale_date.grouper }}
<ul>
{% for sale in sale_list %}
<li>{{ sale.item }} - {{ sale.qty }} </li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
When i render the page i get the grouper sale_date.grouper printed, but {{ sale.item }} and {{ sale.qty }} in the inner loop shows nothing! Blank.
What am i missing?
Gath
{% regroup salecur by sale_date as sale_list %}
<ul>
{% for sale_date in sale_list %}
<li>{{ sale_date.grouper }}
<ul>
{% for sale in sale_date.list %}
<li>{{ sale.item }} - {{ sale.qty }} </li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
See the documentation on regroup:
{% regroup %} produces a list of group objects. Each group object has two attributes:
grouper -- the item that was grouped by (e.g., the string "Male" or "Female").
list -- a list of all items in this group (e.g., a list of all people with gender='Male').
I'm using django-treemenus
I'm curious to see if there is a better way to write out my menu.html which is my menu template. For each level that I add to my menu I have to manually add a level to my menu template.
Here is my menu.html (menu template). It works, but could it be written more efficiently?
{% load tree_menu_tags %}
{% ifequal menu_type "horizontal" %}
<ul><!-- Root -->
{% for menu_item in menu.root_item.children %}
<!-- Level 1-->
{% if menu_item.has_children %}
<li>{{ menu_item.caption }}
<ul>
{% for child in menu_item.children %}
<!-- Level 2-->
{% if child.has_children %}
<li>{{ child.caption }}
<ul>
{% for childchild in child.children %}
<!-- Level 3-->
{% if childchild.has_children %}
<li>{{ childchild.caption }}
<ul>
{% for childchildchild in childchild.children %}
{% show_menu_item childchildchild %}
{% endfor %}
</ul>
</li>
{% else %}
<li>{{ childchild.caption }}</li>
{% endif %}
<!-- End Level 3 -->
{% endfor %}
</ul>
</li>
{% else %}
<li>{{ child.caption }}</li>
{% endif %}
<!-- End Level 2 -->
{% endfor %}
</ul>
</li>
{% else %}
<li>{{ menu_item.caption }}</li>
{% endif %}
<!-- End Level 1 -->
{% endfor %}
</ul><!-- End Root -->
{% endifequal %}
I'd use a custom template tag for this kind of stuff: http://docs.djangoproject.com/en/dev/howto/custom-template-tags/
Here you can find some great examples: http://code.google.com/p/django-page-cms/source/browse/trunk/pages/templatetags/pages_tags.py?r=783