About Allowing Access to a Class-based-view only to Superusers

To allow only superusers to access a certain class-based view, extend this mixin:

1
2
3
4
5
6
7
from django.contrib.auth.mixins import AccessMixin

class SuperUserOnlyMixin(AccessMixin):
    def dispatch(self, request, *args, **kwargs):
        if not request.user.is_superuser:
            return self.handle_no_permission()
        return super().dispatch(request, *args, **kwargs)

Tips and Tricks Programming Django 4.x Django 3.2 Django 2.2 Django 1.11