Debian Codegraph @

docs/usage.rst

raw
1 Usage
2 =====
3
4 1. Once installed, add ``'menu_generator'`` to your INSTALLED\_APPS.
5 2. Add ``{% load menu_generator %}`` to templates that will handle the
6 menus.
7 3. Add the list dictionaries containing the menu items to the settings
8
9 .. code:: python
10
11 ####################################################################################
12 Example: settings.py
13 ####################################################################################
14
15 NAV_MENU_LEFT = [
16 {
17 "name": "Home",
18 "url": "/",
19 },
20 {
21 "name": "About",
22 "url": "/about",
23 },
24 ]
25
26 NAV_MENU_RIGHT = [
27 {
28 "name": "Login",
29 "url": "login_url_view", # reversible
30 "validators": ["menu_generator.validators.is_anonymous"],
31 },
32 {
33 "name": "Register",
34 "url": "register_view_url", # reversible
35 "validators": ["menu_generator.validators.is_anonymous"],
36 },
37 {
38 "name": "Account",
39 "url": "/acount",
40 "validators": ["menu_generator.validators.is_authenticated"],
41 "submenu": [
42 {
43 "name": "Profile",
44 "url": "/account/profile",
45 },
46 {
47 "name": "Account Balance",
48 "url": "/account/balance",
49 "validators": ["myapp.profiles.is_paid_user"],
50 },
51 {
52 "name": "Account Secrets",
53 "url": "/account/secrets",
54 "validators": ["menu_generator.validators.is_superuser"],
55 }
56 ],
57 },
58 ]
59
60 FOOTER_MENU_LEFT = [
61 {
62 "name": "Facebook",
63 "url": "facebook.com/foobar",
64 },
65 {
66 "name": "Contact US",
67 "url": "/contact",
68 },
69 ]
70
71 FOOTER_MENU_RIGHT = [
72 {
73 "name": "Address",
74 "url": "/address",
75 },
76 ]
77
78 Or you can build the menu dictionaries list inside the project apps with
79 ``menus.py`` files, see :doc:`menugeneration` for more.
80
81 4. In your template, load the template tag to generate your menu.
82
83 ::
84
85 {% load menu_generator %}
86 <!DOCTYPE html>
87 <html>
88 <head><title>Django Menu Generator</title></head>
89 <body>
90 <!-- NAV BAR Start -->
91 {% get_menu "NAV_MENU_LEFT" as left_menu %}
92 <div style="float:left;">
93 {% for item in left_menu %}
94 <li class="{% if item.selected %} active {% endif %}">
95 <a href="{{ item.url }}"> <i class="{{ item.icon_class }}"></i> {{ item.name }}</a>
96 </li>
97 {% if item.submenu %}
98 <ul>
99 {% for menu in item.submenu %}
100 <li class="{% if menu.selected %} active {% endif %}">
101 <a href="{{ menu.url }}">{{ menu.name }}</a>
102 </li>
103 {% endfor %}
104 </ul>
105 {% endif %}
106 {% endfor %}
107 </div>
108
109 {% get_menu "NAV_MENU_RIGHT" as right_menu %}
110 <div style="float:right;">
111 {% for item in right_menu %}
112 <li class="{% if item.selected %} active {% endif %}">
113 <a href="{{ item.url }}">{{ item.name }}</a>
114 </li>
115 {% if item.submenu %}
116 <ul>
117 {% for menu in item.submenu %}
118 <li class="{% if menu.selected %} active {% endif %}">
119 <a href="{{ menu.url }}">{{ menu.name }}</a>
120 </li>
121 {% endfor %}
122 </ul>
123 {% endif %}
124 {% endfor %}
125 </div>
126 <!-- NAV BAR End -->
127
128 <!-- Footer Start -->
129 {% get_menu "FOOTER_MENU_LEFT" as left_footer_menu %}
130 <div style="float:left;">
131 <!-- loop through your left footer menus -->
132 </div>
133
134 {% get_menu "FOOTER_MENU_RIGHT" as right_footer_menu %}
135 <div style="float:right;">
136 <!-- loop through your right footer menus -->
137 </div>
138 <!-- Footer End -->
139 </body>
140 </html>
141
142 5. Now you must to see your menus generated when you run your project