[Django]-How do I pass the request object to the method_decorator in django class views?

2👍

A decorator is a function that takes a function (a view in this case), and returns another function (a view in this case). At the moment your rights_needed looks like a regular view – it’s returning a response not a function.

Django comes with a user_passes_test method that makes it easy to create decorators like this. Since you are using class based views, it would be even easier to use the UserPassesTest mixin.

Your test function for the mixin would be:

def test_func(self):
    return self.request.user.groups.filter(Q(name='Admin')).exists()

Leave a comment