简体   繁体   中英

How can I limit access to certain urls depending on the logged in user?

first of all I want to clarify that I am new to this, so if I fail in some concepts I apologize in advance.

I have configured the django administration panel, with an administrator user and two other users that I need to have different permissions to access certain urls of the web environment.

In the home page there are three titles with links to three different sections of the page: SECTION A, SECTION B AND SECTION C.

I want to have a user that has access to all the sections of the page and the other user that has access only to two sections.

How can I manage that?

Another thing I would need is the following: in my configuration, by default, when you launch the web server, the first thing that opens is the django administration portal, but I need that when you log in with a user, access directly to the url that has permissions.

Thanks in advance

Do something like this in your template,

// Section A for all users
{% if user.is_staff %}
    // Section B for staff admin users only
{% elif request.user.has_perm('the_permission_name') or user.is_staff %}
  // Section C for staff or user that has first permission
{% elif request.user.has_perm('the_other_permission_name') or user.is_staff %}
  // Section D for staff or user that has the other permission
{% endif %}

And look at https://docs.djangoproject.com/en/4.0/topics/auth/default/#permissions-and-authorization to set and create permissions.

But I think this question already has an answer here, Check permission inside a template in Django

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM