user3105
user3105

Reputation: 481

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

Upvotes: 1

Views: 707

Answers (1)

raphael
raphael

Reputation: 2880

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

Upvotes: 1

Related Questions