0

I created a custom auth permission in django via admin site, and i added that permission to a user (not a group), now i want to ask if the request user in a template has it but nothing works.It's not a duplicate, i already checked similar questions and none of this work:

{% if perms.auth.add_something %}

{% if 'auth.add_something' in request.user.get_all_permissions %}

{% if request.user.has_perm('add_something') %}

I add my view:

class NotificationSelectView(View):

    template = 'myapp/notification_read.html'

    def get(self, request, *args, **kwargs):
        t = tree(request)
        req_var_list = []
        analist = notifications_list(request.user)
        level = LevelProcess.objects.get(
            level_hierarchical = 3
        )
        subprocess= Process.objects.filter(level = level)
        user = request.user
        t.update({
           'subprocess': subprocess,
           'analist':analist,
        })
        return render(request, self.template, t)

The idea it's to make it inside template, not to use more code in views. Any ideas ?, thanks in advance.

jsanchezs
  • 1,992
  • 3
  • 25
  • 51
  • 1
    show your view. Particularly where does this perms come from? – e4c5 Oct 06 '16 at 23:11
  • I created a permission via admin site, it belongs to auth, but i don't know why it doesn't work, my view doesn't mention perms at all its just assigned to a specific user via admin site too. – jsanchezs Oct 07 '16 at 12:33
  • @e4c5 awright i just added it – jsanchezs Oct 07 '16 at 13:01
  • @jsanchezs make sure you have this context processor in your settings: `django.contrib.auth.context_processors.auth` – Fer Mena Sep 14 '17 at 17:15

1 Answers1

4

django in template it uses the variable perms for the permissions of the logged in user so you can use inside template

{% if perms.auth.add_something %}
 {{do_smth}}
{% endif %}

from the django documentation there is a section Authentication data in templates which describes also permissions in templates

tread
  • 10,133
  • 17
  • 95
  • 170
Dimitris Kougioumtzis
  • 2,339
  • 1
  • 25
  • 36