Ticket #11770: 11770-login-decorator.diff
File 11770-login-decorator.diff, 4.0 KB (added by , 15 years ago) |
---|
-
django/contrib/auth/decorators.py
17 17 return _CheckLogin(view_func, test_func, login_url, redirect_field_name) 18 18 return decorate 19 19 20 def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME ):20 def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=None): 21 21 """ 22 22 Decorator for views that checks that the user is logged in, redirecting 23 23 to the log-in page if necessary. 24 24 """ 25 25 actual_decorator = user_passes_test( 26 26 lambda u: u.is_authenticated(), 27 redirect_field_name=redirect_field_name 27 redirect_field_name=redirect_field_name, 28 login_url=login_url 28 29 ) 29 30 if function: 30 31 return actual_decorator(function) -
docs/topics/auth.txt
664 664 def my_view(request): 665 665 # ... 666 666 667 :func:`~django.contrib.auth.decorators.login_required` also takes an668 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:: 669 669 670 670 from django.contrib.auth.decorators import login_required 671 671 672 672 def my_view(request): 673 673 # ... 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) 675 675 676 676 Again, an equivalent example of the more compact decorator syntax 677 677 introduced in Python 2.4:: … … 684 684 685 685 :func:`~django.contrib.auth.decorators.login_required` does the following: 686 686 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 688 688 :setting:`settings.LOGIN_URL <LOGIN_URL>` (``/accounts/login/`` by 689 default), passing the current absolute URL in the query string as690 `` 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: 691 691 ``/accounts/login/?next=/polls/3/``. 692 692 693 693 * If the user is logged in, execute the view normally. The view code is … … 1020 1020 automatically check that the :class:`~django.contrib.auth.models.User` is 1021 1021 not anonymous. 1022 1022 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). 1026 1028 1027 1029 Example in Python 2.3 syntax:: 1028 1030 … … 1030 1032 1031 1033 def my_view(request): 1032 1034 # ... 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) 1034 1036 1035 1037 Example in Python 2.4 syntax:: 1036 1038 1037 1039 from django.contrib.auth.decorators import user_passes_test 1038 1040 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/') 1040 1042 def my_view(request): 1041 1043 # ... 1042 1044