Django分层模型列表

发布于 2024-12-16 13:00:35 字数 1435 浏览 0 评论 0原文

这应该很简单,但我正在撕碎我的头发只是想思考如何解决它!

我的网站旁边有一个导航菜单,用于选择产品。它的格式如下:

  • 每个产品都属于一个“列表”(单击列表会向您发送一个包含产品列表的页面)。
  • 每个列表都属于一个“类别”(单击该类别会使用 jquery 在单击时展开其下方的列表,但这不是重点)
  • 每个类别都属于一个“类别组”,该组根据不同的内容将类别分为不同的组 简单来说

,产品属于列表,列表属于类别,类别组属于类别组。它们都是一对多的关系。

我需要将其从我的视图传递到我的模板,以便我可以在 HTML 中渲染嵌套列表,以便 jquery 变得漂亮。就像:

<ul>
    <li>Category Group</li>
    <ul>
        <li>Category</li>
        <ul>
            <li>List</li>
            <li>List</li>
            <li>List</li>
        </ul>
        <li>Category</li>
        <ul>
            <li>List</li>
            <li>List</li>
            <li>List</li>
        </ul>
    </ul>
    <li>Category Group</li>
    <ul>
        <li>Category</li>
        <ul>
            <li>List</li>
            <li>List</li>
            <li>List</li>
        </ul>
        <li>Category</li>
        <ul>
            <li>List</li>
            <li>List</li>
            <li>List</li>
        </ul>
    </ul>
</ul>

我的问题是创建所述分层列表以传递给模板,以便它可以呈现它。我知道我需要使用 Model.FK_set.all() 来获取“类别组”中的“类别”列表,但我不知道如何在视图中创建该列表发送到模板的适当方式。有什么帮助吗? Python新手,还在学习中。

This should be simple, but I'm shredding my hair just trying to think how to tackle it!

I have a navigation menu down the side of my site that is used to pick products. It's formatted like so:

  • Every product belongs in a 'list' (clicking on the list sends to you a page with the list of products).
  • Every list belongs in a 'category' (clicking on the category uses jquery to expand out a list beneath it when clicked, but that's besides the point)
  • Every Category belongs in a 'Category Group' that separates the categories into distinct groups depending on what area of the business you're dealing with)

In simpler terms, products belong in a list, which belong in a category, which belongs in a category group. They are all one-to-many relationships.

I need to pass this from my view to my template in such a way as I can render a nested list in HTML for jquery to make pretty. Something like:

<ul>
    <li>Category Group</li>
    <ul>
        <li>Category</li>
        <ul>
            <li>List</li>
            <li>List</li>
            <li>List</li>
        </ul>
        <li>Category</li>
        <ul>
            <li>List</li>
            <li>List</li>
            <li>List</li>
        </ul>
    </ul>
    <li>Category Group</li>
    <ul>
        <li>Category</li>
        <ul>
            <li>List</li>
            <li>List</li>
            <li>List</li>
        </ul>
        <li>Category</li>
        <ul>
            <li>List</li>
            <li>List</li>
            <li>List</li>
        </ul>
    </ul>
</ul>

My issue is creating said hierarchical list to pass to the template so it can render that. I'm aware that I need to use Model.FK_set.all() to get say, a list of 'categories' in a 'category group', but I can't figure out how to create that list in the view in an appropriate way to send to the template. Any help? Python newbie, so still learning the ropes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暖风昔人 2024-12-23 13:00:35

当我看到层次结构或树结构时,我通常会转向 mptt,一种修改后的前序遍历树。谷歌“django mptt”,你会发现大量关于使用 django-mptt 包。

When I see hierarchical or tree structures, I usually turn to mptt, a modified pre-order traversal tree. Google "django mptt" and you will find a large number of tutorials on using the django-mptt package.

薔薇婲 2024-12-23 13:00:35

像这样的东西会起作用:

from __future__ import with_statement
import os
from django.template import Context, Template

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

menu = {
    'food':
        {'fruit':
            ['apple', 'orange'],
        'meat':
            ['beef', 'pork'],
        },
    'animals':
        {'mamals':
             ['cow', 'bear'],
         'reptiles':
             ['frog', 'newt'],
        },
}


with open('template.html', 'r') as infile:
    html = infile.read()
    t = Template(html)

    print t.render(Context({'menu': menu}))

模板:

<html>
<body>
<ul>
{% for name, cat_group in menu.items %}
<li>{{ name }}</li>
    <ul>
    {% for name, cat in cat_group.items %}
    <li>{{ name }}</li>
        <ul>
        {% for product in cat %}
        <li>{{ product }}</li>
        {% endfor %}
        </ul>
    {% endfor %}
    </ul>
{% endfor %}
</ul>
</body>
</html>

Something like this would work:

from __future__ import with_statement
import os
from django.template import Context, Template

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

menu = {
    'food':
        {'fruit':
            ['apple', 'orange'],
        'meat':
            ['beef', 'pork'],
        },
    'animals':
        {'mamals':
             ['cow', 'bear'],
         'reptiles':
             ['frog', 'newt'],
        },
}


with open('template.html', 'r') as infile:
    html = infile.read()
    t = Template(html)

    print t.render(Context({'menu': menu}))

Template:

<html>
<body>
<ul>
{% for name, cat_group in menu.items %}
<li>{{ name }}</li>
    <ul>
    {% for name, cat in cat_group.items %}
    <li>{{ name }}</li>
        <ul>
        {% for product in cat %}
        <li>{{ product }}</li>
        {% endfor %}
        </ul>
    {% endfor %}
    </ul>
{% endfor %}
</ul>
</body>
</html>
罪#恶を代价 2024-12-23 13:00:35

两个类别组的简单示例。

a = [1,2]
b = [3,4,5]
c = [6,7]
d = [8]
item1 = [a,b]
item2 = [c,d]
mygroup = [item1,item2]

在 django 中,您必须使用 for 循环来导航:-

{% for items in mygroup %}
    // Group Category title here
    (% for list in items %}
        // Group title here
            (% for contents in list %}
                  // all the unit contents
[...dont forget to close your for loops...]

A simple example of two category group.

a = [1,2]
b = [3,4,5]
c = [6,7]
d = [8]
item1 = [a,b]
item2 = [c,d]
mygroup = [item1,item2]

In django, you would have to use for loops to navigate:-

{% for items in mygroup %}
    // Group Category title here
    (% for list in items %}
        // Group title here
            (% for contents in list %}
                  // all the unit contents
[...dont forget to close your for loops...]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文