| 1 |
Menu Generation |
| 2 |
=============== |
| 3 |
|
| 4 |
Django Menu Generator uses python dictionaries to represent the menu items, usually a menu item is as follows: |
| 5 |
|
| 6 |
.. code:: python |
| 7 |
|
| 8 |
{ |
| 9 |
"name": 'some name', |
| 10 |
"icon_class": 'some icon class', |
| 11 |
"url": URL spec, |
| 12 |
"root": True | False, |
| 13 |
"related_urls": [ list of related URLs ], |
| 14 |
"related_views": [ list of related views ], |
| 15 |
"validators": [ list of validators ], |
| 16 |
"submenu": Dictionary like this |
| 17 |
} |
| 18 |
|
| 19 |
Where each key is as follows: |
| 20 |
|
| 21 |
- ``name``: A string representing the label of the menu item. If you are using i18n here you can pass the name with the ``ugettext_lazy`` function |
| 22 |
|
| 23 |
- ``icon_class``: A string representing the class of the icon you wish to show on the menu item, e.g you can use font-awesome |
| 24 |
|
| 25 |
- ``url``: See :doc:`urls` |
| 26 |
|
| 27 |
- ``related_urls``: If one of these URLs is part of the path on the currently opened page, the menu item will be marked as selected (format of URLs like described at :doc:`urls`) |
| 28 |
|
| 29 |
- ``related_views``: If the currently opened page resolves to one of these views, the menu item will be marked as selected. |
| 30 |
|
| 31 |
- ``root``: A flag to indicate this item is the root of a path, with this you can correctly mark nested menus as selected. |
| 32 |
|
| 33 |
- ``validators``: See :doc:`validators` |
| 34 |
|
| 35 |
- ``submenu``: You can create infinite nested submenus passing here menu items like this |
| 36 |
|
| 37 |
Django Menu Generator offers two ways to generate the menus, through the Django settings and through each of the Django |
| 38 |
apps |
| 39 |
|
| 40 |
Generating menus through settings |
| 41 |
--------------------------------- |
| 42 |
|
| 43 |
You can add various list dictionaries representing each menu you have as explained in :doc:`usage` |
| 44 |
We recommend to have a ``menus.py`` file with the menu list dictionaries and then import it to the settings file if you |
| 45 |
go this way |
| 46 |
|
| 47 |
Generating menus through apps |
| 48 |
----------------------------- |
| 49 |
|
| 50 |
Some people prefer to isolate all the aspects of a project between apps, so, we add this feature to allow the menus |
| 51 |
live inside each app. |
| 52 |
|
| 53 |
You need to add inside the app a ``menus.py`` file that contains a dictionary called ``MENUS``, each element of the |
| 54 |
dictionary will be a menu list dictionary with all the configuration needed to display that menu, e.g: |
| 55 |
|
| 56 |
.. code:: python |
| 57 |
|
| 58 |
MENUS = { |
| 59 |
'NAV_MENU_LEFT': [ |
| 60 |
{ |
| 61 |
"name": "App1 Feature", |
| 62 |
"url": "/app1-feature" |
| 63 |
} |
| 64 |
], |
| 65 |
'NAV_MENU_TOP': [ |
| 66 |
{ |
| 67 |
"name": "Second Menu Feature", |
| 68 |
"url": "named_url" |
| 69 |
} |
| 70 |
] |
| 71 |
} |
| 72 |
|
| 73 |
So, as an example, for the ``'NAV_MENU_LEFT'``, Django Menu Generator will loop each app searching for the ``'NAV_MENU_LEFT'`` |
| 74 |
list dictionaries inside of the ``MENUS`` and build all the menu configuration to build the whole menu. |
| 75 |
|
| 76 |
With this feature you can have a project structure like this:: |
| 77 |
|
| 78 |
your_project/ |
| 79 |
├── config_folder/ |
| 80 |
│ └── ... |
| 81 |
├── app1 |
| 82 |
│ └── models.py |
| 83 |
│ forms.py |
| 84 |
│ views.py |
| 85 |
│ menus.py |
| 86 |
│ |
| 87 |
├── app2 |
| 88 |
│ └── models.py |
| 89 |
│ forms.py |
| 90 |
│ views.py |
| 91 |
│ menus.py |
| 92 |
│ |
| 93 |
... |
| 94 |
|
| 95 |
You can have a mix of the two approaches if you wish |