File: usage.rst

package info (click to toggle)
django-menu-generator-ng 1.2.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 276 kB
  • sloc: python: 729; makefile: 17
file content (142 lines) | stat: -rw-r--r-- 4,602 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
Usage
=====

1. Once installed, add ``'menu_generator'`` to your INSTALLED\_APPS.
2. Add ``{% load menu_generator %}`` to templates that will handle the
   menus.
3. Add the list dictionaries containing the menu items to the settings

.. code:: python

    ####################################################################################
    Example: settings.py
    ####################################################################################

    NAV_MENU_LEFT = [
        {
            "name": "Home",
            "url": "/",
        },
        {
            "name": "About",
            "url": "/about",
        },
    ]

    NAV_MENU_RIGHT = [
        {
            "name": "Login",
            "url": "login_url_view",  # reversible
            "validators": ["menu_generator.validators.is_anonymous"],
        },
        {
            "name": "Register",
            "url": "register_view_url",  # reversible
            "validators": ["menu_generator.validators.is_anonymous"],
        },
        {
            "name": "Account",
            "url": "/acount",
            "validators": ["menu_generator.validators.is_authenticated"],
            "submenu": [
                {
                    "name": "Profile",
                    "url": "/account/profile",
                },
                {
                    "name": "Account Balance",
                    "url": "/account/balance",
                    "validators": ["myapp.profiles.is_paid_user"],
                },
                {
                    "name": "Account Secrets",
                    "url": "/account/secrets",
                    "validators": ["menu_generator.validators.is_superuser"],
                }
            ],
        },
    ]

    FOOTER_MENU_LEFT = [
        {
            "name": "Facebook",
            "url": "facebook.com/foobar",
        },
        {
            "name": "Contact US",
            "url": "/contact",
        },
    ]

    FOOTER_MENU_RIGHT = [
        {
            "name": "Address",
            "url": "/address",
        },
    ]

Or you can build the menu dictionaries list inside the project apps with
``menus.py`` files, see :doc:`menugeneration` for more.

4. In your template, load the template tag to generate your menu.

::

    {% load menu_generator %}
    <!DOCTYPE html>
    <html>
        <head><title>Django Menu Generator</title></head>
        <body>
            <!-- NAV BAR Start -->
            {% get_menu "NAV_MENU_LEFT" as left_menu %}
            <div style="float:left;">
                {% for item in left_menu %}
                    <li class="{% if item.selected %} active {% endif %}">
                    <a href="{{ item.url }}"> <i class="{{ item.icon_class }}"></i> {{ item.name }}</a>
                    </li>
                    {% if item.submenu %}
                        <ul>
                        {% for menu in item.submenu %}
                            <li class="{% if menu.selected %} active {% endif %}">
                                <a href="{{ menu.url }}">{{ menu.name }}</a>
                            </li>
                        {% endfor %}
                        </ul>
                    {% endif %}
                {% endfor %}
            </div>

            {% get_menu "NAV_MENU_RIGHT" 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>
                    {% if item.submenu %}
                        <ul>
                        {% for menu in item.submenu %}
                            <li class="{% if menu.selected %} active {% endif %}">
                                <a href="{{ menu.url }}">{{ menu.name }}</a>
                            </li>
                        {% endfor %}
                        </ul>
                    {% endif %}
                {% endfor %}
            </div>
            <!-- NAV BAR End -->

            <!-- Footer Start -->
            {% get_menu "FOOTER_MENU_LEFT" as left_footer_menu %}
            <div style="float:left;">
                <!-- loop through your left footer menus -->
            </div>

            {% get_menu "FOOTER_MENU_RIGHT" as right_footer_menu %}
            <div style="float:right;">
                <!-- loop through your right footer menus -->
            </div>
            <!-- Footer End -->
        </body>
    </html>

5. Now you must to see your menus generated when you run your project