Django user permission inside template

a 夏天 提交于 2019-12-12 05:16:21

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/39905210/django-user-permission-inside-template

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