0

I want to make some pages not accessible for some users , what i mean for example i want to make user1 can see a viewa and can not see a viewb, I have tried to do that I have developed 2 functions of permissions and role user function:

Here is the code of the 2 functions of permissions in views.py:

def is_normaluser_login_required(function):
    def wrapper(request, *args, **kw):
        user=request.user  
        if not user.roleuser.is_normaluser:
            return render(request, 'unauthorized.html') # or raise 403
        else:
            return function(request, *args, **kw)
    return wrapper

def is_privilgeuser_login_required(function):
    def wrapper(request, *args, **kw):
        user=request.user  
        if not user.roleuser.is_privilgeuser:
            return render(request, 'unauthorized.html')  # or raise 403
        else:
            return function(request, *args, **kw)
    return wrapper

then i have go to home template where there was a sidebar contain 2 list(every list call a view) , I want to make just the users that are permitted to see this form-list on the navigate bar;

Here is the code in template.html that contain the 2 forms that i want limit the access for both of them:

<nav class="mt-2">
    <ul class="nav" >
      {% if perms.app1_name.is_normaluser_login_required %}
          <li class="nav-item">
            <a href="{%url 'form_module1'%}" class="nav-link">
            </a>
          </li>
      {% endif %}
      {% if perms.app1_name.is_privilgeuser_login_required %}
      <!---->
          <li class="nav-item">
            <a href="{%url 'form_module2'%}" class="nav-link">
            </a>
          </li>
      {% endif %}
    </ul>
  </nav>

---> after that the two lists disappear from the navigate bar (like it doesn't matter if the user have his role or not) otherwise i have mentioned from the admin page the users and their role.

it's like those :

  {% if perms.app_name.is_privilgeuser_login_required %}
      #code...
  {% endif %}

->not workful

I do not if this can be applicable , Thanks in advance.

1 Answers1

0

You can't use decorators in template like that you can create custom filter or template tag.
Instead of creating custom filter/tags you can do this by request.
Change your code like this this

<nav class="mt-2">
    <ul class="nav" >
      {% if user.roleuser.is_normaluser %}
          <li class="nav-item">
            <a href="{%url 'form_module1'%}" class="nav-link">
            </a>
          </li>
      {% endif %}
      {% if user.roleuser.is_privilgeuser %}
      <!---->
          <li class="nav-item">
            <a href="{%url 'form_module2'%}" class="nav-link">
            </a>
          </li>
      {% endif %}
    </ul>
  </nav>
Ankit Tiwari
  • 4,438
  • 4
  • 14
  • 41