my django -простое меню не работает. Мой код очень похож на пример из doku https://django-simple-menu.readthedocs.io/en/latest/usage.html#usage -example .
Страница загружается без ссылок меню, и я не получаю сообщений об ошибках. Буду признателен за любые идеи, как решить эту проблему.
Django: 3.0.1
Python: 3.7.5
nav. html
<nav class="navbar sticky-top navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavDropdown">
<ul class="navbar-nav">
{% for item in menu %}
<li class="nav-item active">
<a class="nav-link" href="{{ item.url }}">{{ item.title }} <span class="sr-only">(current)</span></a>
</li>
{% endfor %}
</ul>
</div>
</nav>
основание. html
{% load menu %}
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
...
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static '/css/base.css' %}">
</head>
<body>
{% if user.is_authenticated %}
<div class="d-flex bg-dark header " >
<div class="p-2 flex-grow-1 bg-dark "> </div>
<div class="p-2 bg-dark ">User: {{ user.username }} </div>
<div class="p-2 bg-dark"><a href="{% url 'logout' %}">Logout</a></div>
</div>
{% generate_menu %}
{% with menu=menus.main %}{% include 'nav.html' %}{% endwith %}
{% else %}
....
{% endif %}
<main>
...
</main>
</body>
</html>
урегулирование .py
"""
Django settings for nsap project.
Generated by 'django-admin startproject' using Django 3.0.1.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '+hari9jxke*m!4os*6gc!4ygk_hr5es-qdx)x=qza7)7m&x&%='
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'userManagement.apps.UsermanagementConfig',
'doppik.apps.DoppikConfig',
'base.apps.BaseConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'menu',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
BASE_DIR + '/nsap/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',
],
},
},
]
WSGI_APPLICATION = 'wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# USER Model
#AUTH_USER_MODEL = 'core.User'
# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Europe/Berlin'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
PROJECT_NAME = 'nsap'
STATIC_ROOT = os.path.join(BASE_DIR, PROJECT_NAME, 'staticfiles')
STATIC_URL = os.path.join(BASE_DIR, PROJECT_NAME, 'static/')
#MEDIA_URL = '/media/'
#MEDIA_ROOT = os.path.abspath(os.path.join(BASE_DIR, '..','..', 'media'))
# Loging
LOGIN_REDIRECT_URL = '/'
menus.py
from menu import Menu, MenuItem
from django.urls import reverse
Menu.add_item("main", MenuItem("Booking",
reverse("doppik.views.booking"),
weight=10,
icon="tools"))
Menu.add_item("main", MenuItem("ShowAccount",
reverse("doppik.views.show_accounts"),
weight=10,
icon="tools"))
Menu.add_item("main", MenuItem("Login",
reverse('django.contrib.auth.views.login'),
check=lambda request: not request.user.is_authenticated()))
Menu.add_item("main", MenuItem("Logout",
reverse('django.contrib.auth.views.logout'),
check=lambda request: request.user.is_authenticated()))
Редактировать: я думаю, проблема в том, что menus.py не запущен и никакие элементы MenuItem не добавлены в Menu.
У меня есть добавил команду print ("foo") в файл menus.py. Выход не отображается в терминале.