- August 27, 2009
- decorator auth groups
- 3 (after 3 ratings)
This snippet provides a @group_required decorator. You can pass in multiple groups, for example:
@group_required('admins','editors') def myview(request, id): ...
It is important to check that the user is first logged in, as anonymous users trigger an AttributeError when the groups filter is executed.
1 2 3 4 5 6 7 8 9 10
from django.contrib.auth.decorators import user_passes_test def group_required(*group_names): """Requires user membership in at least one of the groups passed in.""" def in_groups(u): if u.is_authenticated(): if bool(u.groups.filter(name__in=group_names)) | u.is_superuser: return True return False return user_passes_test(in_groups)
More like this
- Automatically setup raw_id_fields ForeignKey & OneToOneField by agusmakmun 8 months, 1 week ago
- Crispy Form by sourabhsinha396 9 months ago
- ReadOnlySelect by mkoistinen 9 months, 2 weeks ago
- Verify events sent to your webhook endpoints by santos22 10 months, 1 week ago
- Django Language Middleware by agusmakmun 10 months, 3 weeks ago