Reputation: 2403
My django based website will have 3 seperate menus. The items of first one are: contact, about, disclosures. The second one will have: terms and condtions, privacy policy, copyright. And items of main menu are: Home, link1, link2, link2.... The first two menus will have fixed items, and the items of last one may change. As I will be using forloop in the template, what is the best approach for creating those menus. The webpages will have just a title and content.
Upvotes: 0
Views: 1284
Reputation: 18387
You can stay DRY and just use django-menuware. It supports nested menus as well.
Install:
pip install django-menuware
# Add `menuware` to your settings.py**
# Add `MENUWARE_MENU` to your settings.py:**
Settings:
MENUWARE_MENU = {
"RIGHT_NAV_MENU": [
{ # Show `Login` to `unauthenticated` users ONLY
"name": "Login",
"url": "/login/",
"render_for_unauthenticated": True,
},
{ # Show `Logout` to `authenticated` users ONLY
"name": "Logout",
"url": "/logout/",
"render_for_authenticated": True,
},
{ # Show `Search` to all users
"name": "Search",
"url": "/search/",
"render_for_unauthenticated": True,
"render_for_authenticated": True,
},
],
"LEFT_NAV_MENU": [
{ # Show `Admin` to `superuser` ONLY
"name": "Admin",
"url": "admin:index", # Reversible
"render_for_authenticated": True,
"render_for_superuser": True,
},
{ # Show `Comment Admin` to `staff` users ONLY
"name": "Comment Admin",
"url": "/review/admin/",
"render_for_authenticated": True,
"render_for_staff": True,
},
]
Usage:
<!-- base.html -->
{% load menuware %}
<!DOCTYPE html>
<html>
<head><title>Django Menuware</title></head>
<body>
<!-- NAV BAR Start -->
{% get_menu "LEFT_NAV_MENU" as left_menu %}
<div style="float:left;">
{% for item in left_menu %}
<li class="{% if item.selected %} active {% endif %}">
<a href="{{item.url}}">{{item.name}}</a>
</li>
{% endfor %}
</div>
{% get_menu "RIGHT_NAV_MENU" as right_menu %}
<div style="float:right;">
{% for item in right_menu %}
<li class="{% if item.selected %} active {% endif %}">
<a href="{{item.url}}">{{item.name}}</a>
</li>
{% endfor %}
</div>
<!-- NAV BAR End -->
</body>
</html>
Minimally, you'd want to look at its Github README page before rolling your own.
Upvotes: 0
Reputation: 45284
I like to use inclusion template tags for dynamic menus.
In my-app/templatetags/myappmenu.py
, I have something like:
from django import template
register = template.Library()
@register.inclusion_tag('my-app/menu.html')
def myappmenu():
return [("label1", "link1"), ("label2", "link2")]
Then, in your template you can loop over the items and produce the menu in the format you desire (<p>
, <ul>
, etc.).
If you need to make items in the menu appear conditionally, you can add them to the list by checking permissions in the template tag; just pass the request or user object as argument to the template tag function.
Upvotes: 1