Dynamic User Menu in Django

无人久伴 提交于 2020-01-01 14:27:32

问题


Is there a way to have a user menu that changes according to the permissions assigned to the user group a user belongs to? I am thinking of something that checks for these permissions at the view level, and also removes menu options a user does not have permission to.


回答1:


Yes it is possible to access the user object in the template and check if the user is staff like this:

{% if user.is_staff %}
    <li>
        <a href="/admin/">Admin</a>
    </li>
{% endif %}

This would be an example where your menu where li items of links. The admin link would only be rendered for users with is_staff status. The same could be done with is_authenticated.

Django is build to have logic and presentation separated, so if you want to do some more fine grained control of the menu, I would suggest doing the logic inside of the view, and then set a variable that you can check in the template to determine which menus to show.




回答2:


For the most part, django's admin already doesnt give you links to things you can't do.

Django grappelli (a django admin skin) implements some sort of bookmarking, if that is what you mean http://code.google.com/p/django-grappelli/



来源:https://stackoverflow.com/questions/1050653/dynamic-user-menu-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!