2014-04-22 47 views
0

有沒有辦法找到某個羣組模型擁有的權限?如何請求羣組權限

我在 「views.py」 有這個

def assign_permissions(request, id_group): 
    group = Group.objects.get(id=id_group) 
    perms = Permission.objects.all() 
    ctx = {'group':group, 'permissions':perms} 
    return render_to_response('home/group/assign_perm.html', ctx, context_instance=RequestContext(request)) 

這裏是我的html代碼

<div class="jumbotron"> 
    <div class="bs-example"> 
     <h1><small>Permissions</small></h1> 
     {% if permissions %} 
      <div class="table-responsive"> 
       <table class="table table-hover"> 
        <thead> 
         <tr> 
          <th>ID</th> 
          <th>name</th> 
          <th>codename</th> 
          <th></th> 
         </tr> 
        </thead> 
        <tbody> 
         {% for p in permissions %} 
          <tr> 
           <td>{{ p.id }}</td> 
           <td>{{ p.name }}</td> 
           <td>{{ p.codename }}</td> 
            {% if group.has_perm(p.codename) %} 
             <td><a class="btn btn-sm btn-default" href="#"><span class="glyphicon glyphicon-plus"></span> Deny</a></td> 
            {% else %} 
             <td><a class="btn btn-sm btn-default" href="#"><span class="glyphicon glyphicon-plus"></span> Assign</a></td> 
            {% endif %} 
          </tr> 
         {% endfor %} 
        </tbody> 
       </table> 
      </div> 
     {% else %} 
      <h3>No permissions.</h3> 
     {% endif %} 
    </div> 
    <br> 
    <a class="btn btn-default" href="{% url 'show_groups' %}"><span class="glyphicon glyphicon-chevron-left"></span> Return</a> 
</div> 

我知道{% if group.has_perm(p.codename) %}句話是錯誤的。我就是這樣寫的,以瞭解我正在嘗試做什麼。

回答

2

可以使用group.permissions.all在模板中訪問一組的權限,

參考你的榜樣,你是否一組具有使用特定權限:

{% for p in permissions %} 
    {% if p in group.permissions.all %} 
     ...