【问题标题】:Load HTML if User belongs to a certain group如果用户属于某个组,则加载 HTML
【发布时间】:2013-05-16 20:39:44
【问题描述】:

我只想在用户属于某个组时才显示链接。

html:

{% if user.is_authenticated %}
    {% users_in_group = Group.objects.get(name="boss").user_set.all() %}
    {% if user in users_in_group %}
    <li><a href='/register'>Create User</a></li>
    {% endif %}
{% endif %}

非常感谢

【问题讨论】:

    标签: html django django-templates


    【解决方案1】:

    为了解决这个问题,我做了以下操作

    查看:

    if request.user.is_authenticated():
        a = request.user.groups.count()
        if a > 0:
            #a = request.user.groups.all()[0] #Trae el nombre del grupo 
            a = "jefe"
        else:
            a = "no user groups"    
    else:
        a = "user is not log"
    

    base.html:

      {% if user.is_authenticated %}
        {% if a in "jefes" %}
          <li><a href='/usuarios'>Usuarios</a></li>
        {% endif %}   
      {% endif %}
    

    我不知道这是否是最好的,但我服务过。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多