hi, i did the same. My solution is not as elegant but works ok for me:
model:
class Menu(models.Model):
createdate = models.DateTimeField(auto_now_add='True')
islive = models.BooleanField(default=True, help_text="Untick if you
do not want this to be live.")
position = models.IntegerField(help_text="enter a digit for manual
sorting, 0 is 1st.")
name = models.CharField(_("Name"), core=True, maxlength=19)
httplink = models.CharField(maxlength=250, blank = True, null=True)
parent = models.ForeignKey('self', limit_choices_to =
{ 'parent__isnull':'True' }, editable=True, blank = True, null=True,
related_name='child')
def __str__(self):
return self.name
class Admin:
list_display = ( 'position', 'name', 'parent')
pass
parent can point to it self, with the limit_choices_to i made it so
that you can only chose menu points with out a parent i.e. Root menu,
so this goes 2 levels deep.
Than i use a template tag to call the menu:
register = template.Library()
@register.inclusion_tag('cms/menu.html')
def menugroup(parent):
menu = Menu.objects.filter(parent__isnull=parent,
islive=True).order_by('position')
return {'menu': menu}
@register.inclusion_tag('cms/submenu.html')
def submenugroup(parentid):
menu = Menu.objects.filter(parent=parentid,
islive=True).order_by('position')
return {'menu': menu}
and here is the template:
menu.html (put the UL tag around it in the main template that calls
this tag, i use some fancy CSS to make it all look nice, its a
vertical flyout menu)
{% load menu %}
{% for menu in menu %}
{% if forloop.first %}
{% else %}
{% if forloop.last %}
{% else %}
{% endif %}
{% endif %}
{{ menu.name }}
{% submenugroup menu.id %}
{% endfor %}
submenu.html
{% for menu in menu %}
{{ menu.name }}
{% endfor %}
much more elegant would be to build the menu in the templeate tag
completly, if you look at the django ecommece shop Satchmo, there is a
much nicer example.
http://www.satchmoproject.com/trac/browser/satchmo/trunk/satchmo/shop/views
<- i thinks in there some where.
I did this when i started with django and python, so my skills were
very limited, still not much better ;) but getting there.
On Jun 5, 10:42 pm, tyman26 <[EMAIL PROTECTED]> wrote:
> I am trying to build dynamic menus that are brought in from a
> database. I set up the table in this structure:
> CREATE TABLE auth_menu
> (
> menu_id serial NOT NULL,
> group_id int4 NOT NULL,
> parent_id int4,
> auth_permission_id int4,
> title varchar(35) NOT NULL,
> url varchar(100),
> order_index int4 NOT NULL,
> .)
>
> The top level menu is known by the fact that menu_id and group_id are
> the same with a null parent_id, and then it goes parent, child If
> the menu_id and group_id are the same, then we know its also a top
> level menu, but still underneath the main_menu. If its a bottom level
> leaf, then none are the same. I am stuck on how to build html with ul/
> li lists or if it's even possible. I am certain recursion is needed.
> Does anyone have an idea where to look for a solution to this problem?
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---