как используя нижеследующее Installation & Configuration: pip install django-menu Add…
- User
как используя нижеследующее Installation & Configuration: pip install django-menu
Add menu to your INSTALLED_APPS
./manage.py migrate menu (or ./manage.py syncdb if you don't use South. You should use South.)
Add django.template.context_processors.request to your TEMPLATE settings. Below is a reasonably safe TEMPLATES setting for most small projects, however yours may vary.:
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', 'django.template.context_processors.request', ], }, }, ] Add a Menu (eg called headernavigation) and add some items to that menu
In your template, load the menu tags and embed your primary menu.
In your template, instead of the {% menu %} tag use {% submenu %}. If a submenu for the current URI exists, it will be shown. The {{ submenu_items }} list contains your navigation items, ready to output like in the examples above.
Caching: To avoid hitting the database every time a user requests a page, the menu items are cached if you have a cache configured. Caching is not used when settings.DEBUG is True.
To disable caching, set the setting MENU_CACHE_TIME to -1 or remove your Django Cache configuration.
To enable caching to continue to let you make items available to anonymous or authenticated users, and to enable the "Current Page" functionality, the cache will contain one dataset for each menu, authentication & path combination. создать основное меню МЕНЮ и субменю СУБМЕНЮ. Пример кода с пояснениями
- Assistant
- User
как с помощью django-menu сделать нормальное двух уровневое меню на основе {% load menubuilder %}{% submenu %}{% if submenu %} {{ submenu.name }} » <a href="{{ submenu.base_url }}" title="{{ submenu.name|escape }}"{% elif submenu.base_url request.path %} class='current'{% endif %}>{{ submenu.name }} {% for item in submenu_items %} <a href="{{ item.url }}" title="{{ item.title|escape }}"{% if item.current %} class='current'{% endif %}>{{ item.title }} {% endfor %} {% endif %} пример кода с пояснеиями
- Assistant
Пример кода для создания двухуровневого меню с использованием пакета django-menu:
python{% load menubuilder %} {% submenu %} {% if submenu %} <div id='subnav'> <h2>{{ submenu.title }}</h2> <ul> <li>» <a href="{{ submenu.base_url }}" title="{{ submenu.title|escape }}" {% if submenu.base_url == request.path %}class='current'{% endif %}>{{ submenu.title }}</a></li> {% for item in submenu_items %} <li><a href="{{ item.url }}" title="{{ item.title|escape }}" {% if item.current %}class='current'{% endif %}>{{ item.title }}</a></li> {% endfor %} </ul> </div> {% endif %}