class UserPassesTestMixin  
When using class-based views, you can use the UserPassesTestMixin to do this.
- 
test_func() - 
You have to override the
test_func()method of the class to provide the test that is performed. Furthermore, you can set any of the parameters ofAccessMixinto customize the handling of unauthorized users:from django.contrib.auth.mixins import UserPassesTestMixin class MyView(UserPassesTestMixin, View): def test_func(self): return self.request.user.email.endswith('@example.com') 
- 
get_test_func() - 
You can also override the
get_test_func()method to have the mixin use a differently named function for its checks (instead oftest_func()). 
Stacking UserPassesTestMixin
Due to the way UserPassesTestMixin is implemented, you cannot stack them in your inheritance list. The following does NOT work:
class TestMixin1(UserPassesTestMixin):
    def test_func(self):
        return self.request.user.email.endswith('@example.com')
class TestMixin2(UserPassesTestMixin):
    def test_func(self):
        return self.request.user.username.startswith('django')
class MyView(TestMixin1, TestMixin2, View):
    ...
 If TestMixin1 would call super() and take that result into account, TestMixin1 wouldn’t work standalone anymore.
Please login to continue.