Changeset 3779
- Timestamp:
- 09/21/06 20:44:28 (2 years ago)
- Files:
-
- django/trunk/django/contrib/auth/decorators.py (modified) (1 diff)
- django/trunk/docs/authentication.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/contrib/auth/decorators.py
r3478 r3779 27 27 """ 28 28 ) 29 30 def permission_required(perm, login_url=LOGIN_URL): 31 """ 32 Decorator for views that checks if a user has a particular permission 33 enabled, redirectiing to the log-in page if necessary. 34 """ 35 return user_passes_test(lambda u: u.has_perm(perm), login_url=login_url) 36 django/trunk/docs/authentication.txt
r3747 r3779 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 be 460 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. 462 459 463 Here's the same thing, using Python 2.4's decorator syntax:: 460 464 … … 488 492 def my_view(request): 489 493 # ... 494 495 The permission_required decorator 496 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 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:: 502 503 from django.contrib.auth.decorators import permission_required 504 505 def my_view(request): 506 # ... 507 508 my_view = permission_required('polls.can_vote')(my_view) 509 510 Note that ``permission_required()`` also takes an optional ``login_url`` 511 parameter. 490 512 491 513 Limiting access to generic views
