Класс AccessMixin (Django 3.0)

from django.contrib.auth.mixins import AccessMixin

Abstract CBV mixin that gives access mixins the same customizable functionality.

Атрибуты

  Определено в
login_url = None AccessMixin
permission_denied_message = '' AccessMixin
raise_exception = False AccessMixin
redirect_field_name = 'next' AccessMixin

Методы

Override this method to override the login_url attribute.

def get_login_url(self):
    """
    Override this method to override the login_url attribute.
    """
    login_url = self.login_url or settings.LOGIN_URL
    if not login_url:
        raise ImproperlyConfigured(
            '{0} is missing the login_url attribute. Define {0}.login_url, settings.LOGIN_URL, or override '
            '{0}.get_login_url().'.format(self.__class__.__name__)
        )
    return str(login_url)

Override this method to override the permission_denied_message attribute.

def get_permission_denied_message(self):
    """
    Override this method to override the permission_denied_message attribute.
    """
    return self.permission_denied_message

Override this method to override the redirect_field_name attribute.

def get_redirect_field_name(self):
    """
    Override this method to override the redirect_field_name attribute.
    """
    return self.redirect_field_name
def handle_no_permission(self):
    if self.raise_exception or self.request.user.is_authenticated:
        raise PermissionDenied(self.get_permission_denied_message())
    return redirect_to_login(self.request.get_full_path(), self.get_login_url(), self.get_redirect_field_name())