python - Django Navagation with dropdowns -
this templatetag file:
@register.inclusion_tag("site/nav_bar.html") def nav_bar(): nav_bar = page.objects.filter(navbar=true, parent=page.objects.filter(slug="home"),).exclude(slug="home",).order_by('order')[:20] return { "nav_bar": nav_bar } navbar html:
<ul class="left"> <li><a href="/">home</a></li> {% nav in nav_bar %} <li><a href="/{{ nav.slug }}">{{ nav.title }}</a></li> {% endfor %} </ul> part of models.py
class page(models.model): title = models.charfield( max_length=100, blank=true, ) slug = models.charfield( max_length=100, unique=true, blank=true, ) parent = models.foreignkey( ("page"), blank=true, null=true, ) how add dropdowns main navagation bar, i'm not talking html/css getting loop inside (i'm using foundation zurb)
you have menu designed tree pointers parent (also knows adjacency list model). calculate tree, need perform sql query each level build tree (this inefficient). article above gives strageties how query data build tree.
you might want @ storing menu higher order data model mpttmodel django-mptt.
i recommend checking out open source project implements menuing system
django-cms example , has robust menu system. store pages mpttmodels , build tree querying pages. menu app uses structure pages render content out through template tags
this should give idea how implement yourself
Comments
Post a Comment