2

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.

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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>

Comments

0

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...]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.