V
V
Vasily2015-11-25 19:19:17
Django
Vasily, 2015-11-25 19:19:17

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]

Then a list for the role is given to the menu and it is formed, something like this
<ul>
    {% for perm in perms %}
        <li><a href="{{ perm.url }}">{{ perm.name }}</a></li>
    {% endfor %}
</ul>

Is this practiced at all? What is the correct way to implement such things?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Kitaev, 2015-11-25
@dedik

Add a menu to a function, a function to CONTEXT_PROCESSORS and, based on request.user, determine what to return.

V
v- death, 2015-11-25
@vGrabko99

I would advise to form a menu on the client side

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question