Answer the question
In order to leave comments, you need to log in
How to make a dynamic menu in django?
Good afternoon, such a problem - the site will have a menu to access the sections.
You need to display sections depending on the role.
The menu is bootstrap and it is desirable to draw it somehow automatically
. Here are a few questions - how can I create a list of roles and the corresponding url to which they have access and how to actually form this menu? And where is the best place to do it?
The menu should be displayed for all sections that inherit the basic personal account template.
A little more - I think it should look like this - there is a dictionary, there the key value is the user's role, and the value itself is a list of urls where the role has access, something like this
perm = {
'CLIENT': {
'home': {'url': reversed('home'), 'name': 'Главная'},
'orders': {'url': reversed('orders'), 'name': 'Заказы'},
'etc': {'url': reversed('etc'), 'name': 'Остальное'}
},
'ADMIN': {
'admin': {'url': reversed('admin'), 'name': 'Управление'},
'home': {'url': reversed('home'), 'name': 'Главная'},
'orders': {'url': reversed('orders'), 'name': 'Заказы'},
'etc': {'url': reversed('etc'), 'name': 'Остальное'}
}
}
...
role = 'CLIENT'
...
return perm[role]
<ul>
{% for perm in perms %}
<li><a href="{{ perm.url }}">{{ perm.name }}</a></li>
{% endfor %}
</ul>
Answer the question
In order to leave comments, you need to log in
Add a menu to a function, a function to CONTEXT_PROCESSORS and, based on request.user, determine what to return.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question