0

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
SAFEER N
  • 1,157
  • 2
  • 17
  • 30

2 Answers2

1

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.

Community
  • 1
  • 1
Agate
  • 3,152
  • 1
  • 19
  • 30
1

Try to fetch user's permissions this way:

user_permissions = user.user_permissions.all()
Eugene Soldatov
  • 9,755
  • 2
  • 35
  • 43