Menu Generico using Python, Django and tree structure

Asked

Viewed 386 times

1

I am creating a generic menu where a parent can have 1 child and so on (structure of a tree) and I have arisen a doubt of how to render the parents in a menu and when pass the mouse over the parent open laterally their children

inserir a descrição da imagem aqui

Currently I only have this code that takes the parents and children all together, I would like to leave everything separated but I do not know which way to go.

def details(request,pk):

item = Item.objects.filter(fkmenu_id=pk)

template_name = 'menu/menu.html'
context = {
'items': item
}
return render(request,template_name,context)

py.models inserir a descrição da imagem aqui

I’m using mptt which is a lib to work with trees but so far nothing, in case someone has some way without the mptt also serves.

  • Okay, but that code doesn’t help much, put the class code Item, are persisted? in what way? how do you resolve the tree?

  • In theory the node is the one that has the parent_id=null so in the template I can list by doing a for in the item and seeing if the item.parent_id is None but I wanted to also get this guy’s son.

  • I’m developing a Django app that uses this lib, in case I’m presenting a api tree-shaped, you can see the result here, on the Heroku and the implementation and description here at github

  • In addition to developing the models, q vc has tried?

  • I managed to solve my problem by making a method to catch the children and dps made 1 inside another to render in the template

  • Solved the whole problem or still missing something?

  • I managed to solve my problem completely.

  • Create a recursive menu class that is called on each menu https://github.com/jazzband/django-simple-menu

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.