Ticket #903: patch_login_required.diff
File patch_login_required.diff, 3.6 KB (added by , 16 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 login_url, 28 redirect_field_name 28 29 ) 29 30 if function: 30 31 return actual_decorator(function) 31 32 return actual_decorator 32 33 33 def permission_required(perm, login_url=None ):34 def permission_required(perm, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME): 34 35 """ 35 36 Decorator for views that checks whether a user has a particular permission 36 37 enabled, redirecting to the log-in page if necessary. 37 38 """ 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) 39 40 40 41 class _CheckLogin(object): 41 42 """ -
docs/topics/auth.txt
673 673 @login_required(redirect_field_name='redirect_to') 674 674 def my_view(request): 675 675 # ... 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 # ... 676 696 677 697 :func:`~django.contrib.auth.decorators.login_required` does the following: 678 698 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: 683 704 ``/accounts/login/?next=/polls/3/``. 684 705 685 706 * If the user is logged in, execute the view normally. The view code is … … 994 1015 995 1016 As in the :func:`~decorators.login_required` decorator, ``login_url`` 996 1017 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``. 997 1022 998 1023 Limiting access to generic views 999 1024 --------------------------------