Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documenting use of get_obj_perms when looping over an object list within template #424

Open
kierer opened this issue Apr 29, 2016 · 3 comments

Comments

@kierer
Copy link

kierer commented Apr 29, 2016

I am using a class-based view (ListView) and have code that is structured as follows in the template. The permission check is returning false when I would expect the delete_widget permission to be present in the widget_perms for objects in the collection for the logged-in user. Am I using get_obj_perms incorrectly within a looping structure? If so, might an example of how to use it correctly when looping over an object list in a template be added to the docs?

{% load guardian_tags %}
{% block content %}
    {% if object_list %}
        {for widget in object_list %}
            {% get_obj_perms request.user for widget as "widget_perms" %}
            {% if "delete_widget" in "widget_perms" %}
                <a href="/widget/delete/{{ widget.pk }}" class="btn btn-danger" role = "button">Delete</a>
            {% endif %}
        {% endfor %}
    {% endif %}
{% endblock %}

Thanks!
K

@ad-m
Copy link
Member

ad-m commented Apr 30, 2016

See #382.

@kierer
Copy link
Author

kierer commented May 2, 2016

Thank you! I hadn't realized that prefetch_perms support had been added to guardian_tags for use in templates (I was going by the docs and thought only get_obj_perms was available). I'll give it a shot tonight.

@kierer
Copy link
Author

kierer commented May 3, 2016

It looks like prefetch_perms isn't (yet) supported in guardian_tags. I just went with a less elegant approach- adding a deletable_widgets collection to the context with a get_context_data override on the (class-based) view, then in the template

{% if widget in deletable_widgets %}
                <a href="/widget/delete/{{ widget.pk }}" class="btn btn-danger" role = "button">Delete</a>
{% endif %}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants