Reputation: 48
I am editing the user permissions in templates. And I want to display checked checkbox if user has permissions or unchecked if user has not specific permissions.
My View.py codes are as below.
def EditUserView(request,id=0):
user = User.objects.get(pk=id)
permission = Permission.objects.all()
return render(request,'edituser.html',{"userdata":user,"permissions":permission})
And My templates code are as given below.
<div class="table-wrapper">
<table class="table">
<thead>
<tr>
<th>Permissions</th>
<th>Status</th>
</tr>
</thead>
<tbody>
{% for perm in permissions%}
<tr>
<td id="perm">{{perm}}</td>
<td><input type="checkbox" name="status" id="status" onclick="ChangeUserPermissions(this,userid='{{userdata.id}}',permission='{{perm}}')"></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
My objectives of the code is only display checked checkbox if have permissions or unchecked if not have permissions
Upvotes: 0
Views: 432
Reputation: 1412
So you need to register custome tag for this it can be filter:
@register.filter
def has_perm(user, perm):
return user.has_perm(perm)
then in template:
{% for perm in permissions%}
<tr>
<td>{{perm}}</td>
<td><input type="checkbox" name="status" onclick="ChangeUserPermissions(this,userid='{{userdata.id}}',permission='{{perm}}')"
{% if user|has_perm:perm %} selected {% endif %}>
</td>
</tr>
{% endfor %}
those ids on td tags wont be uniqe
Upvotes: 1