class UserPassesTestMixin
from django.contrib.auth.mixins import UserPassesTestMixin
Deny a request with a permission error if the test_func() method returns False.
Ancestors (MRO)
- UserPassesTestMixin
- AccessMixin
Attributes
Defined in | |
---|---|
login_url = None
|
AccessMixin |
permission_denied_message = ''
|
AccessMixin |
raise_exception = False
|
AccessMixin |
redirect_field_name = 'next'
|
AccessMixin |
Methods
def
dispatch(self, request, *args, **kwargs):
UserPassesTestMixin
¶
def
dispatch(self, request, *args, **kwargs):
UserPassesTestMixin
¶
105 106 107 108 109 | def dispatch(self, request, *args, **kwargs):
user_test_result = self.get_test_func()()
if not user_test_result:
return self.handle_no_permission()
return super().dispatch(request, *args, **kwargs)
|
def
get_login_url(self):
AccessMixin
¶
def
get_login_url(self):
AccessMixin
¶
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)
|
def
get_permission_denied_message(self):
AccessMixin
¶
def
get_permission_denied_message(self):
AccessMixin
¶
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
|
def
get_redirect_field_name(self):
AccessMixin
¶
def
get_redirect_field_name(self):
AccessMixin
¶
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
|
def
get_test_func(self):
UserPassesTestMixin
¶
def
get_test_func(self):
UserPassesTestMixin
¶
Override this method to use a different test_func method.
99 100 101 102 103 | def get_test_func(self):
"""
Override this method to use a different test_func method.
"""
return self.test_func
|
def
handle_no_permission(self):
AccessMixin
¶
def
handle_no_permission(self):
AccessMixin
¶
41 42 43 44 | 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())
|
def
test_func(self):
UserPassesTestMixin
¶
def
test_func(self):
UserPassesTestMixin
¶
94 95 96 97 | def test_func(self):
raise NotImplementedError(
'{} is missing the implementation of the test_func() method.'.format(self.__class__.__name__)
)
|