我想检查组在模板中是否有权限进行循环。
我的上下文处理器
from django.contrib.auth.models import User,Group,Permission
from django.db.models import Q
def users(request):
users = User.objects.filter(is_active=1).exclude(id=request.user.id)
groups = Group.objects.exclude(Q(name='customer') | Q(name='vendor') | Q(name='labour'))
permissions = Permission.objects.all()
return {
'all_users': users,
'all_groups' : groups,
'permissions' : permissions
}
我的模板
<table>
<tr>
<th>Permission</th>
<th>Content Type</th>
<th>Access Group</th>
</tr>
{% for permission in permissions %}
<tr class="item-row">
<td>
{{permission.name}}
<input type="hidden" name="permission_{{permission.id}}" />
</td>
<td>
{{permission.content_type.app_label}}
</td>
<td>
{% for group in all_groups %}
<span class="checkboxes fln">
<span class="checkbox">
<small class="single checkNo">
<input type="checkbox" name="group_{{group.id}}_permission_{{permission.id}}"/>
</small>
<b>{{group.name}}</b>
</span>
</span>
{% endfor %}
</td>
</tr>
{% endfor %}
</table>
如果群组有权限,我想更改此行。
<input type="checkbox" name="group_{{group.id}}_permission_{{permission.id}}" {%if 'condition' %} checked="checked" {% endif %}/>
如何检查群组是否拥有该权限。
答案 0 :(得分:1)
群组权限属于权限:
来自the docs
<强>权限强>
允许的多对多字段:
所以你可以这么做:
{% if permission in group.permissions.all %} checked="checked" {% endif %}