Thanks to arie, the answer helped a long way, but it doesn't work for me.
When I found this snippet, I got it to work properly: http://djangosnippets.org/snippets/983/
This solution worked for me:
The helper function
This function has the benefit of being reusable in other places, as a drop in replacement for user.is_authenticated
. It could for instance be exposed as a template tag.
def my_custom_authenticated(user):
if user:
if user.is_authenticated():
return user.groups.filter(name=settings.MY_CUSTOM_GROUP_NAME).exists()
return False
The decorator
I just put this at the top of my views.py
, since it's so short.
def membership_required(fn=None):
decorator = user_passes_test(my_custom_authenticated)
if fn:
return decorator(fn)
return decorator
Using it
@membership_required
def some_view(request):
...