Ticket #11770: 11770-login-decorator.diff

File 11770-login-decorator.diff, 4.0 KB (added by Michael Newman, 15 years ago)

Patch with desired (assumed) functionality and updated docs

  • django/contrib/auth/decorators.py

     
    1717        return _CheckLogin(view_func, test_func, login_url, redirect_field_name)
    1818    return decorate
    1919
    20 def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME):
     20def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=None):
    2121    """
    2222    Decorator for views that checks that the user is logged in, redirecting
    2323    to the log-in page if necessary.
    2424    """
    2525    actual_decorator = user_passes_test(
    2626        lambda u: u.is_authenticated(),
    27         redirect_field_name=redirect_field_name
     27        redirect_field_name=redirect_field_name,
     28        login_url=login_url
    2829    )
    2930    if function:
    3031        return actual_decorator(function)
  • docs/topics/auth.txt

     
    664664        def my_view(request):
    665665            # ...
    666666
    667     :func:`~django.contrib.auth.decorators.login_required` also takes an
    668     optional ``redirect_field_name`` parameter. Example::
     667    :func:`~django.contrib.auth.decorators.login_required` also takes optional   
     668    ``redirect_field_name`` or ``login_url`` parameters. Example::
    669669
    670670        from django.contrib.auth.decorators import login_required
    671671
    672672        def my_view(request):
    673673            # ...
    674         my_view = login_required(redirect_field_name='redirect_to')(my_view)
     674        my_view = login_required(redirect_field_name='redirect_to', login_url='login_url')(my_view)
    675675
    676676    Again, an equivalent example of the more compact decorator syntax
    677677    introduced in Python 2.4::
     
    684684
    685685    :func:`~django.contrib.auth.decorators.login_required` does the following:
    686686
    687         * If the user isn't logged in, redirect to
     687        * If the user isn't logged in, redirect to ``login_url`` if specified or
    688688          :setting:`settings.LOGIN_URL <LOGIN_URL>` (``/accounts/login/`` by
    689           default), passing the current absolute URL in the query string as
    690           ``next`` or the value of ``redirect_field_name``. For example:
     689          default), passing the current absolute path or the value of
     690          ``redirect_field_name`` in the query string as ``next``. For example:
    691691          ``/accounts/login/?next=/polls/3/``.
    692692
    693693        * If the user is logged in, execute the view normally. The view code is
     
    10201020    automatically check that the :class:`~django.contrib.auth.models.User` is
    10211021    not anonymous.
    10221022
    1023     :func:`~django.contrib.auth.decorators.user_passes_test()` takes an
    1024     optional ``login_url`` argument, which lets you specify the URL for your
    1025     login page (:setting:`settings.LOGIN_URL <LOGIN_URL>` by default).
     1023    :func:`~django.contrib.auth.decorators.user_passes_test()` takes optional
     1024    ``redirect_field_name`` or ``login_url`` arguments. ``redirect_field_name``
     1025    sets the ``next`` parameter in the query string (the absolute path by
     1026    default). ``login_url`` specifies the URL for your login page
     1027    (:setting:`settings.LOGIN_URL <LOGIN_URL>` by default).
    10261028
    10271029    Example in Python 2.3 syntax::
    10281030
     
    10301032
    10311033        def my_view(request):
    10321034            # ...
    1033         my_view = user_passes_test(lambda u: u.has_perm('polls.can_vote'), login_url='/login/')(my_view)
     1035        my_view = user_passes_test(lambda u: u.has_perm('polls.can_vote'), redirect_field_name='redirect_to', login_url='/login/')(my_view)
    10341036
    10351037    Example in Python 2.4 syntax::
    10361038
    10371039        from django.contrib.auth.decorators import user_passes_test
    10381040
    1039         @user_passes_test(lambda u: u.has_perm('polls.can_vote'), login_url='/login/')
     1041        @user_passes_test(lambda u: u.has_perm('polls.can_vote'), redirect_field_name='redirect_to', login_url='/login/')
    10401042        def my_view(request):
    10411043            # ...
    10421044
Back to Top