Changeset 3835
- Timestamp:
- 09/25/06 12:33:17 (2 years ago)
- Files:
-
- django/trunk/django/contrib/auth/decorators.py (modified) (1 diff)
- django/trunk/docs/authentication.txt (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/contrib/auth/decorators.py
r3779 r3835 30 30 def permission_required(perm, login_url=LOGIN_URL): 31 31 """ 32 Decorator for views that checks ifa user has a particular permission33 enabled, redirecti ing to the log-in page if necessary.32 Decorator for views that checks whether a user has a particular permission 33 enabled, redirecting to the log-in page if necessary. 34 34 """ 35 35 return user_passes_test(lambda u: u.has_perm(perm), login_url=login_url) django/trunk/docs/authentication.txt
r3793 r3835 457 457 my_view = user_passes_test(lambda u: u.has_perm('polls.can_vote'))(my_view) 458 458 459 We are using this particular test as a relatively simple example, however be460 aware that if you just want to test if a permission is available to a user, 461 you can use the ``permission_required()`` decorator described below.459 We're using this particular test as a relatively simple example. However, if 460 you just want to test whether a permission is available to a user, you can use 461 the ``permission_required()`` decorator, described later in this document. 462 462 463 463 Here's the same thing, using Python 2.4's decorator syntax:: … … 496 496 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 497 497 498 Since checking whether a user has a particular permission available to them is a 499 relatively common operation, Django provides a shortcut for that particular 500 case: the ``permission_required()`` decorator. Using this decorator, the 501 earlier example can be written as:: 498 **New in Django development version** 499 500 It's a relatively common task to check whether a user has a particular 501 permission. For that reason, Django provides a shortcut for that case: the 502 ``permission_required()`` decorator. Using this decorator, the earlier example 503 can be written as:: 502 504 503 505 from django.contrib.auth.decorators import permission_required 504 505 def my_view(request): 506 # ... 507 506 507 def my_view(request): 508 # ... 508 509 my_view = permission_required('polls.can_vote')(my_view) 509 510 510 511 Note that ``permission_required()`` also takes an optional ``login_url`` 511 parameter. 512 parameter. Example:: 513 514 from django.contrib.auth.decorators import permission_required 515 516 def my_view(request): 517 # ... 518 my_view = permission_required('polls.can_vote', login_url='/loginpage/')(my_view) 519 520 As in the ``login_required`` decorator, ``login_url`` defaults to 521 ``'/accounts/login/'``. 512 522 513 523 Limiting access to generic views … … 634 644 635 645 {% if user.is_authenticated %} 636 <p>Welcome, {{ user.username }}. Thanks for logging in.</p> 646 <p>Welcome, {{ user.username }}. Thanks for logging in.</p> 637 647 {% else %} 638 648 <p>Welcome, new user. Please log in.</p>
