Reputation: 1197
I want to edit user permissions.
My View
def edit_roles(request,username):
user = User.objects.get(username=username)
user_permissions = user.get_all_permissions()
permissions = Permission.objects.filter(Q(codename='can_create_product') | Q(codename='can_view_product')
return render_to_response('users/edit_user_roles.html',{'permissions':permissions,'user_permissions':user_permissions},context_instance=RequestContext(request))
My template
<table>
<tr>
<th>Permission</th>
<th>Access</th>
</tr>
{% for permission in permissions %}
<tr class="item-row">
<td>
{{permission.name}}
</td>
<td>
<input type="checkbox" name="" {% if permission in user_permissions %} checked="checked" {% endif %} />
</td>
</tr>
{% endfor %}
</table>
Now I got user permissions. but {% if permission in user_permissions %}
not working. Please suggest me a good way to solve this.
Upvotes: 0
Views: 2452
Reputation: 10135
Try to fetch user's permissions this way:
user_permissions = user.user_permissions.all()
Upvotes: 1
Reputation: 3232
Maybe you can replace user_permissions
by request.user.get_all_permissions
? (if you want to get permissions for the User sending the request).
You can get more info about this method in the docs.
However, according to this previous StackOverflow anwser, there may be a more efficient way to check permissions in template.
Upvotes: 1