Side menu does not load in custom template #464
-
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Ok, I already know that I need to send 'available_apps' in context dictionary which will contain the list of applications. But how do I get this list? I try to do this using the get_app_list method from classy AdminSite found in django/contrib/admin/sites.py but I get an empty list. from django.contrib.admin.sites import AdminSite
def change_view(self, request, category, part_type, part_id):
...
context = {
...
'available_apps': AdminSite().get_app_list(request)
}
return render(request, 'parts/part_form.html', context=context) |
Beta Was this translation helpful? Give feedback.
-
Okay, I solved this problem a bit around the corner. def get_menu_list() -> Dict:
""" Get menu list to use in side_menu admin template and index admin template. """
menu_list = {} # type: Dict[str, List]
settings_head_icon = 'fas fa-cogs'
menu_list['Settings'] = [
{
'name': 'Users',
'changelist_url': reverse('admin:auth_user_changelist'),
'icon': 'fas fa-user-cog',
'head_icon': settings_head_icon,
'view_permissions': ['auth.view_user']
},
{
'name': 'Grups',
'changelist_url': reverse('admin:auth_group_changelist'),
'icon': 'fas fa-users',
'head_icon': settings_head_icon,
'view_permissions': ['auth.view_group']
}]
return menu_list
def extra_context_procesor(request):
""" Context processor which adds to each rendered page a menu_list to context.
It needs to be added in the settings.py file as in the example below.
TEMPLATES = [{
...
'OPTIONS': {
'context_processors': [
...
'parts.menu_list.extra_context_procesor'
],},},]
"""
return {'menu_list': get_menu_list()} And I override base.html templates like this: <li class="nav-item">
<a href="{% url 'admin:index' %}" class="nav-link">
<i class="nav-icon fas fa-home"></i>
<p>Dashboard</p>
</a>
</li>
{% if jazzmin_settings.navigation_expanded %}
{% for header, app in menu_list.items %}
<li class="nav-header">{{ header }}</li>
{% for model in app %}
<li class="nav-item">
{% if model.changelist_url %}
<a href="{{ model.changelist_url }}" class="nav-link">
<i class="nav-icon {{ model.icon }}"></i> <p>{{ model.name }}</p>
</a>
{% else %}
<span class="nav-link disabled">
<i class="nav-icon {{ model.icon }}"></i> <p>{{ model.name }}</p>
</span>
{% endif %}
</li>
{% endfor %}
{% endfor %}
{% else %}
{% for header, app in menu_list.items %}
<li class="nav-item has-treeview">
<a href="#" class="nav-link">
<i class="nav-icon {{ app.0.head_icon }}"></i>
<p>{{ header }} <i class="fas fa-angle-left right"></i></p>
</a>
<ul class="nav nav-treeview" style="display: none;">
{% for model in app %}
<li class="nav-item">
<a href="{% if model.changelist_url %}{{ model.changelist_url }}{% else %}javascript:void(0){% endif %}" class="nav-link">
<i class="nav-icon {{ model.icon }}"></i>
<p>{{ model.name }}</p>
</a>
</li>
{% endfor %}
</ul>
</li>
{% endfor %}
{% endif %} |
Beta Was this translation helpful? Give feedback.
Okay, I solved this problem a bit around the corner.
I defined a function that returns a dictionary with menu items, and I creat an additional context processor for it.