[1.2.X] Fixed #8325 -- Reorganization and expansion of the login_required decorator docs to make it clearer how the redirect_field_name parameter works and improve the overall flow of the text. Thanks to Robert Reeves for the report.

Backport of [14480] from trunk (sans 1.3-specific changes).

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@14481 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Gabriel Hurley 2010-11-07 01:00:05 +00:00
parent d8e0052d45
commit 2946a657cd
1 changed files with 23 additions and 20 deletions

View File

@ -693,7 +693,7 @@ login page::
The login_required decorator The login_required decorator
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. function:: decorators.login_required() .. function:: decorators.login_required([redirect_field_name=REDIRECT_FIELD_NAME])
As a shortcut, you can use the convenient As a shortcut, you can use the convenient
:func:`~django.contrib.auth.decorators.login_required` decorator:: :func:`~django.contrib.auth.decorators.login_required` decorator::
@ -704,32 +704,35 @@ The login_required decorator
def my_view(request): def my_view(request):
... ...
:func:`~django.contrib.auth.decorators.login_required` also takes an
optional ``redirect_field_name`` parameter. Example::
from django.contrib.auth.decorators import login_required
@login_required(redirect_field_name='redirect_to')
def my_view(request):
...
:func:`~django.contrib.auth.decorators.login_required` does the following: :func:`~django.contrib.auth.decorators.login_required` does the following:
* If the user isn't logged in, redirect to * If the user isn't logged in, redirect to
:setting:`settings.LOGIN_URL <LOGIN_URL>` (``/accounts/login/`` by :setting:`settings.LOGIN_URL <LOGIN_URL>`, passing the current absolute
default), passing the current absolute URL in the query string. The path in the query string. Example: ``/accounts/login/?next=/polls/3/``.
name of the GET argument is determined by the ``redirect_field_name``
argument provided to the decorator. The default argument name is
``next``. For example:
``/accounts/login/?next=/polls/3/``.
* If the user is logged in, execute the view normally. The view code is * If the user is logged in, execute the view normally. The view code is
free to assume the user is logged in. free to assume the user is logged in.
Note that you'll need to map the appropriate Django view to By default, the path that the user should be redirected to upon
:setting:`settings.LOGIN_URL <LOGIN_URL>`. For example, using the defaults, add successful authentication is stored in a query string parameter called
the following line to your URLconf:: ``"next"``. If you would prefer to use a different name for this parameter,
:func:`~django.contrib.auth.decorators.login_required` takes an
optional ``redirect_field_name`` parameter::
from django.contrib.auth.decorators import login_required
@login_required(redirect_field_name='my_redirect_field')
def my_view(request):
...
If you provide a value to ``redirect_field_name``, you will most
likely need to customize your login template as well, since the template
context variable which stores the redirect path will use the value of
``redirect_field_name`` as it's key rather than ``"next"`` (the default).
Note that you'll need to map the appropriate Django view to
:setting:`settings.LOGIN_URL <LOGIN_URL>`. For example, using the defaults,
add the following line to your URLconf::
(r'^accounts/login/$', 'django.contrib.auth.views.login'), (r'^accounts/login/$', 'django.contrib.auth.views.login'),