Ticket #903: patch_login_required.diff

File patch_login_required.diff, 3.6 KB (added by vbmendes, 6 years ago)

Adds login_url parameter for login_required decorator and redirect_field_name parameter for permission_required decorator.

  • 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        login_url,
     28        redirect_field_name
    2829    )
    2930    if function:
    3031        return actual_decorator(function)
    3132    return actual_decorator
    3233
    33 def permission_required(perm, login_url=None):
     34def permission_required(perm, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
    3435    """
    3536    Decorator for views that checks whether a user has a particular permission
    3637    enabled, redirecting to the log-in page if necessary.
    3738    """
    38     return user_passes_test(lambda u: u.has_perm(perm), login_url=login_url)
     39    return user_passes_test(lambda u: u.has_perm(perm), login_url, redirect_field_name)
    3940
    4041class _CheckLogin(object):
    4142    """
  • docs/topics/auth.txt

     
    673673        @login_required(redirect_field_name='redirect_to')
    674674        def my_view(request):
    675675            # ...
     676   
     677    :func:`~django.contrib.auth.decorators.login_required` also takes an
     678    optional ``login_url`` argument, which lets you specify the URL for
     679    your login page (settings.LOGIN_URL by default).
     680   
     681    Example in Python 2.3 syntax::
     682   
     683        from django.contrib.auth.decorators import login_required
     684       
     685        def my_view(request):
     686            # ...
     687        my_view = login_required(login_url='/login/')(my_view)
     688   
     689    Example in Python 2.4 syntax::
     690   
     691        from django.contrib.auth.decorators import login_required
     692       
     693        @login_required(login_url='/login/')
     694        def my_view(request):
     695            # ...
    676696
    677697    :func:`~django.contrib.auth.decorators.login_required` does the following:
    678698
    679         * If the user isn't logged in, redirect to
    680           :setting:`settings.LOGIN_URL <LOGIN_URL>` (``/accounts/login/`` by
    681           default), passing the current absolute URL in the query string as
    682           ``next`` or the value of ``redirect_field_name``. For example:
     699        * If the user isn't logged in, redirect to ``login_url`` value, wich
     700          defaults to :setting:`settings.LOGIN_URL <LOGIN_URL>`
     701          (``/accounts/login/`` by default), passing the current absolute URL
     702          in the query string as ``next`` or the value of
     703          ``redirect_field_name``. For example:
    683704          ``/accounts/login/?next=/polls/3/``.
    684705
    685706        * If the user is logged in, execute the view normally. The view code is
     
    9941015
    9951016    As in the :func:`~decorators.login_required` decorator, ``login_url``
    9961017    defaults to :setting:`settings.LOGIN_URL <LOGIN_URL>`.
     1018   
     1019    :func:`~decorators.permission_required` decorator, also as in the
     1020    :func:`~decorators.login_required` decorator, takes an optional parameter
     1021    ``redirect_field_name``.
    9971022
    9981023Limiting access to generic views
    9991024--------------------------------
Back to Top