Refs #24914 -- Added docs for more auth mixin methods.

This commit is contained in:
Tim Graham 2015-08-20 10:16:09 -04:00
parent 01966bb2a7
commit 6c6eb8a691
1 changed files with 43 additions and 16 deletions

View File

@ -607,17 +607,25 @@ redirects to the login page::
When using :doc:`class-based views </topics/class-based-views/index>`, you When using :doc:`class-based views </topics/class-based-views/index>`, you
can use the ``UserPassesTestMixin`` to do this. can use the ``UserPassesTestMixin`` to do this.
You have to override the ``test_func()`` method of the class to provide .. method:: test_func()
the test that is performed. Furthermore, you can set any of the parameters
of :class:`~django.contrib.auth.mixins.AccessMixin` to customize the
handling of unauthorized users::
from django.contrib.auth.mixins import UserPassesTestMixin 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 of :class:`~django.contrib.auth.mixins.AccessMixin` to
customize the handling of unauthorized users::
class MyView(UserPassesTestMixin, View): from django.contrib.auth.mixins import UserPassesTestMixin
def test_func(self): class MyView(UserPassesTestMixin, View):
return self.request.user.email.endswith('@example.com')
def test_func(self):
return self.request.user.email.endswith('@example.com')
.. method:: 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 of
:meth:`test_func`).
.. admonition:: Stacking ``UserPassesTestMixin`` .. admonition:: Stacking ``UserPassesTestMixin``
@ -741,20 +749,19 @@ user to the login page or issue an HTTP 403 Forbidden response.
.. attribute:: login_url .. attribute:: login_url
The URL that users who don't pass the test will be redirected to. Default return value for :meth:`get_login_url`. Defaults to ``None``
Defaults to :setting:`settings.LOGIN_URL <LOGIN_URL>`. in which case :meth:`get_login_url` falls back to
:setting:`settings.LOGIN_URL <LOGIN_URL>`.
.. attribute:: permission_denied_message .. attribute:: permission_denied_message
When ``raise_exception`` is ``True``, this attribute can be used to Default return value for :meth:`get_permission_denied_message`.
control the error message passed to the error handler for display to Defaults to an empty string.
the user. Defaults to an empty string.
.. attribute:: redirect_field_name .. attribute:: redirect_field_name
The name of the query parameter that will contain the URL the user Default return value for :meth:`get_redirect_field_name`. Defaults to
should be redirected to after a successful login. If you set this to ``"next"``.
``None``, a query parameter won't be added. Defaults to ``"next"``.
.. attribute:: raise_exception .. attribute:: raise_exception
@ -762,6 +769,26 @@ user to the login page or issue an HTTP 403 Forbidden response.
:class:`~django.core.exceptions.PermissionDenied` exception will be :class:`~django.core.exceptions.PermissionDenied` exception will be
raised instead of the redirect. Defaults to ``False``. raised instead of the redirect. Defaults to ``False``.
.. method:: get_login_url()
Returns the URL that users who don't pass the test will be redirected
to. Returns :attr:`login_url` if set, or :setting:`settings.LOGIN_URL
<LOGIN_URL>` otherwise.
.. method:: get_permission_denied_message()
When :attr:`raise_exception` is ``True``, this method can be used to
control the error message passed to the error handler for display to
the user. Returns the :attr:`permission_denied_message` attribute by
default.
.. method:: get_redirect_field_name()
Returns the name of the query parameter that will contain the URL the
user should be redirected to after a successful login. If you set this
to ``None``, a query parameter won't be added. Returns the
:attr:`redirect_field_name` attribute by default.
.. method:: handle_no_permission() .. method:: handle_no_permission()
Depending on the value of ``raise_exception``, the method either raises Depending on the value of ``raise_exception``, the method either raises