Django分层模型列表
这应该很简单,但我正在撕碎我的头发只是想思考如何解决它!
我的网站旁边有一个导航菜单,用于选择产品。它的格式如下:
- 每个产品都属于一个“列表”(单击列表会向您发送一个包含产品列表的页面)。
- 每个列表都属于一个“类别”(单击该类别会使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当我看到层次结构或树结构时,我通常会转向 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.
像这样的东西会起作用:
模板:
Something like this would work:
Template:
两个类别组的简单示例。
在 django 中,您必须使用 for 循环来导航:-
A simple example of two category group.
In django, you would have to use for loops to navigate:-