根据所选父母显示子节点 [英] show children nodes depending on selected parent

查看:177
本文介绍了根据所选父母显示子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找,找不到答案。我只有3个月的使用python / django的经验,所以借口我的虚疑!
我使用django mptt显示一个简单的嵌套集导航。

 < ul class =root> 
{%recursetree nodes%}
< li>
{{node.name}}
{%if not node.is_leaf_node%}
< ul class =children>
{{children}}
< / ul>
{%endif%}
< / li>
{%endrecursetree%}



这个功能很好 - 但是我只想显示所选类别的孩子(基于slug),而不是全部显示。
任何想法?






我终于这样做了:

  {%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%}

在视图

  category = get_object_or_404(Category,slug = slug)
child = category.get_children()
如果不是孩子:
child = category.get_siblings ()

但它是一个黑客。有人有更好的主意吗?

解决方案

您需要发送关于您所在节点的一些信息,然后是一个简单的如果语句。



关于如何普遍地发送节点信息,有几种方法可以在Django中执行此操作,没有他们是完美的。我的首选方法是上下文处理器: http ://docs.djangoproject.com/en/1.3/ref/templates/api/#writing-your-own-context-processors


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

这篇关于根据所选父母显示子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆