动态导航在django [英] Dynamic navigation in django

查看:345
本文介绍了动态导航在django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个动态导航菜单,它会根据数据库自动填写,并且会在每个页面上。例如,我有一个流派模型。在这个模型中有一个称为父母的字段。如果这是null,它是一个顶级的类型。否则它拥有其他类型的id,并且是这种类型的子代。它看起来像这样:

I want to build a dynamic navigation menu that will fill in automatically based on the database, and will be on every page. For example, I have a Genre model. In this model there is a field called parent. If this is null, it is a top-level genre. Otherwise it holds the id of other genre, and is a subgenre of this genre. It looks like this:

class Genre(models.Model):
    name = models.CharField(max_length=100)
    slug = models.SlugField(max_length=100)
    parent = models.ForeignKey('self', null=True, blank=True)

def __str__(self):
    return self.name

所以我有一个CSS下拉菜单,我想填充它就像这样:

So I have a CSS dropdown menu and I want to populate it so it goes like this:

类型 - >顶级类型列表。然后对于每一个,这个类型的子类的另一个下拉菜单。如果这是有道理的。

Genre -> List of top-level genres. Then for each of these, another drop-down menu of the subgenres of that genre. If that makes sense.

但是我不知道如何在django中实现。我有一个上下文处理器,但这只是将所有类型列为当前的一个平面下拉列表(我必须记住为每个视图添加它)。我不知道使用自定义标签或包含标签是否会更好,或者如何使用此示例。

But I have no idea how to implement this in django. I have a context processor, but this just lists all genres as one flat drop-down currently (and I have to remember to add it for every view). I'm not sure if it would be better to use a custom tag or an inclusion tag, or how to do it for this example.

谢谢。

推荐答案

你正在谈论创建一个层次结构/树,这是非常烦人的处理。

You're talking about creating a hierarchy / tree, which is rather annoying to deal with.

查看修改预订树遍历,一种处理这样的树的方法。

有一个django应用程序!
http://django-mptt.github.com/django-mptt/

Look into Modified Preorder Tree Traversal, a method for dealing with trees like this.
There's a django app for it! http://django-mptt.github.com/django-mptt/

我强烈建议您使用这种方式,而不是滚动您自己的。

I highly recommend using this instead of rolling your own.

这是一个快速入门教程:比你自己做的容易得多
http://komunitasweb.com/2010/ 09 / a-quick-tutorial-on-django-mptt /

Here's a quick tutorial on getting started: so much easier than doing this yourself. http://komunitasweb.com/2010/09/a-quick-tutorial-on-django-mptt/

否则,如果你想支持无限深度,您将需要一个递归函数,它通过类型的子代进行迭代。以下是 Satchmo 购物车的示例。基本上,您必须遍历每个顶级父项,递归迭代其子节点,直到不再有子节点。

Otherwise, if you want to support infinite depth, you will need a recursive function that iterates through a Genre's children. Here's an example from the Satchmo shopping cart. Basically, you'd have to iterate over each top level parent, recursively iterate over its children until there are no more children.

def _recurse_for_children(self, node, only_active=False):
    children = []
    children.append(node)
    for child in node.child.active():
        if child != self:
            if (not only_active) or child.active_products().count() > 0:
                children_list = self._recurse_for_children(child, only_active=only_active)
                children.append(children_list)
    return children

我强烈建议您使用satchmo的模板标签来渲染这些树,以获得一些想法。

I would highly recommend looking at satchmo's template tag for rendering these trees to get some ideas.

让我知道你有任何问题。

Let me know if you have any questions.

这篇关于动态导航在django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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