模板中的Django用户权限 [英] Django user permission inside template

查看:207
本文介绍了模板中的Django用户权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过管理站点在django中创建了一个自定义身份验证权限,并将该权限添加到了一个用户(不是一个组),现在我想问一问模板中的请求用户是否拥有它,但是没有用.重复,我已经检查了类似的问题,但没有这项工作:

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') %}

我添加我的视图:

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.

推荐答案

django在模板中,它使用变量perms来获取登录用户的权限,因此您可以在模板内部使用

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 %}

django文档中有一个

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

这篇关于模板中的Django用户权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆