【问题标题】:Django Grappelli usergroup conditional dashboardDjango Grappelli 用户组条件仪表板
【发布时间】:2018-07-25 13:13:26
【问题描述】:

在 Django(1.11.10) 中使用 Grappelli(2.11.1)。 我制作了小型自定义仪表板,现在寻找一些方法来改变元素的外观。 特别是我需要显示一些 url 元素

self.children.append(modules.LinkList(
    _('e-mail'),
    column=2,
    css_class=('grp-collapse grp-closed'),
    children=[
        {
            'title': _('gmail'),
            'url': 'gmail.com',
            'external': True,
        },
    ]
))

仅对指定组的用户和对其他人隐藏。 以常规方式,我会尝试使用 auth.get_user(),但这取决于“请求”。这对我来说是不可用的,或者至少是不可见的。 有什么办法可以实现这个功能? 还是谢谢。

【问题讨论】:

    标签: django admin dashboard usergroups


    【解决方案1】:

    一种可能的解决方法(尽管几乎可以肯定有更好的方法):

    首先,将'django.template.context_processors.request' 添加到settings.TEMPLATES['OPTIONS']['context_processors'](如果它不存在)。这会将请求添加到模板上下文。

    接下来,在 init_with_context 方法中对自定义仪表板进行更改,您现在可以访问请求(以及 request.user)

    from grappelli.dashboard import modules, Dashboard
    
    class CustomDashboard(Dashboard):
    
        def init_with_context(self, context):
          request = context['request']
          user = request.user
          groups = user.groups.all()
          if groups == "whatever condition you want":
            self.children.append(
                ...
            )
    

    【讨论】:

    • 是的。我在仪表板脚本中获得了用户对象。谢谢。
    猜你喜欢
    • 2011-06-15
    • 1970-01-01
    • 2016-06-28
    • 1970-01-01
    • 2016-06-05
    • 2015-07-07
    • 1970-01-01
    • 2018-07-11
    • 2015-10-01
    相关资源
    最近更新 更多