Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #24253 -- Documented staff_member_required decorator #4286

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/ref/contrib/admin/index.txt
Expand Up @@ -2717,3 +2717,26 @@ The action in the examples above match the last part of the URL names for
:class:`ModelAdmin` instances described above. The ``opts`` variable can be any
object which has an ``app_label`` and ``model_name`` attributes and is usually
supplied by the admin views for the current model.

.. currentmodule:: django.contrib.admin.views.decorators

.. _staff_member_required_ref:

The staff_member_required decorator
===================================

.. function:: staff_member_required([redirect_field_name=REDIRECT_FIELD_NAME, login_url='admin:login'])

:func:`~django.contrib.admin.views.decorators.staff_member_required` does
the following:

* If the user is logged in, is a staff member (meaning that user
object's property ``is_staff=True``), and is active
(``is_active=True``), execute the view normally.

* Otherwise, redirect to :setting:`settings.LOGIN_URL
<LOGIN_URL>`, passing the current absolute path in the query string.
Example: ``/accounts/login/?next=/polls/3/``.

For usage notes, see the documentation on :ref:`Staff member required usage
<topics-staff_member_required>`.
28 changes: 28 additions & 0 deletions docs/topics/auth/default.txt
Expand Up @@ -487,6 +487,34 @@ The login_required decorator

The login_required decorator does NOT check the is_active flag on a user.

.. _topics-staff_member_required:

The staff_member_required decorator
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

As a shortcut, you can use
:func:`~django.contrib.admin.views.decorators.staff_member_required` decorator::

from django.contrib.admin.views.decorators import staff_member_required

@staff_member_required
def my_view(request):
...

:func:`~django.contrib.admin.views.decorators.staff_member_required` takes
two optional arguments: ``redirect_field_name`` and ``login_url`` -- their
effect and default values are the same as ``login_required`` decorator --
see :func:`~django.contrib.auth.decorators.login_required`.

See :func:`~django.contrib.admin.views.decorators.staff_member_required`
for more details.

.. note::

Unlike the login_required decorator, staff_member_required DOES check the
is_active flag on a user.


Limiting access to logged-in users that pass a test
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down