Everything you wanted to know
about the Django framework

Класс PermissionRequiredMixin (Django 2.0)

from django.contrib.auth.mixins import PermissionRequiredMixin

Verify that the current user has all specified permissions.

Предки (MRO)

  1. PermissionRequiredMixin
  2. AccessMixin

Атрибуты

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

Методы

82
83
84
85
def dispatch(self, request, *args, **kwargs):
    if not self.has_permission():
        return self.handle_no_permission()
    return super().dispatch(request, *args, **kwargs)
Override this method to override the login_url attribute.
17
18
19
20
21
22
23
24
25
26
27
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.
29
30
31
32
33
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 permission_required attribute.
Must return an iterable.
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def get_permission_required(self):
    """
    Override this method to override the permission_required attribute.
    Must return an iterable.
    """
    if self.permission_required is None:
        raise ImproperlyConfigured(
            '{0} is missing the permission_required attribute. Define {0}.permission_required, or override '
            '{0}.get_permission_required().'.format(self.__class__.__name__)
        )
    if isinstance(self.permission_required, str):
        perms = (self.permission_required, )
    else:
        perms = self.permission_required
    return perms
Override this method to override the redirect_field_name attribute.
35
36
37
38
39
def get_redirect_field_name(self):
    """
    Override this method to override the redirect_field_name attribute.
    """
    return self.redirect_field_name
41
42
43
44
def handle_no_permission(self):
    if self.raise_exception:
        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())
Override this method to customize the way permissions are checked.
75
76
77
78
79
80
def has_permission(self):
    """
    Override this method to customize the way permissions are checked.
    """
    perms = self.get_permission_required()
    return self.request.user.has_perms(perms)
Auth mixins
Auth views
generic
Generic base
Generic dates
Generic detail
Generic edit
Generic list