Removed docs that assume developer might be using Python < 2.4
git-svn-id: http://code.djangoproject.com/svn/django/trunk@12400 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
4bff194633
commit
eaee55e547
|
@ -138,8 +138,7 @@ The ``Library.filter()`` method takes two arguments:
|
||||||
2. The compilation function -- a Python function (not the name of the
|
2. The compilation function -- a Python function (not the name of the
|
||||||
function as a string).
|
function as a string).
|
||||||
|
|
||||||
If you're using Python 2.4 or above, you can use ``register.filter()`` as a
|
You can use ``register.filter()`` as a decorator instead::
|
||||||
decorator instead::
|
|
||||||
|
|
||||||
@register.filter(name='cut')
|
@register.filter(name='cut')
|
||||||
@stringfilter
|
@stringfilter
|
||||||
|
@ -557,8 +556,7 @@ The ``tag()`` method takes two arguments:
|
||||||
2. The compilation function -- a Python function (not the name of the
|
2. The compilation function -- a Python function (not the name of the
|
||||||
function as a string).
|
function as a string).
|
||||||
|
|
||||||
As with filter registration, it is also possible to use this as a decorator, in
|
As with filter registration, it is also possible to use this as a decorator::
|
||||||
Python 2.4 and above::
|
|
||||||
|
|
||||||
@register.tag(name="current_time")
|
@register.tag(name="current_time")
|
||||||
def do_current_time(parser, token):
|
def do_current_time(parser, token):
|
||||||
|
@ -657,7 +655,7 @@ Our earlier ``current_time`` function could thus be written like this::
|
||||||
|
|
||||||
register.simple_tag(current_time)
|
register.simple_tag(current_time)
|
||||||
|
|
||||||
In Python 2.4, the decorator syntax also works::
|
The decorator syntax also works::
|
||||||
|
|
||||||
@register.simple_tag
|
@register.simple_tag
|
||||||
def current_time(format_string):
|
def current_time(format_string):
|
||||||
|
@ -738,8 +736,7 @@ loader, we'd register the tag like this::
|
||||||
# Here, register is a django.template.Library instance, as before
|
# Here, register is a django.template.Library instance, as before
|
||||||
register.inclusion_tag('results.html')(show_results)
|
register.inclusion_tag('results.html')(show_results)
|
||||||
|
|
||||||
As always, Python 2.4 decorator syntax works as well, so we could have
|
As always, decorator syntax works as well, so we could have written::
|
||||||
written::
|
|
||||||
|
|
||||||
@register.inclusion_tag('results.html')
|
@register.inclusion_tag('results.html')
|
||||||
def show_results(poll):
|
def show_results(poll):
|
||||||
|
|
|
@ -696,36 +696,19 @@ The login_required decorator
|
||||||
|
|
||||||
from django.contrib.auth.decorators import login_required
|
from django.contrib.auth.decorators import login_required
|
||||||
|
|
||||||
def my_view(request):
|
|
||||||
# ...
|
|
||||||
my_view = login_required(my_view)
|
|
||||||
|
|
||||||
Here's an equivalent example, using the more compact decorator syntax
|
|
||||||
introduced in Python 2.4::
|
|
||||||
|
|
||||||
from django.contrib.auth.decorators import login_required
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def my_view(request):
|
def my_view(request):
|
||||||
# ...
|
...
|
||||||
|
|
||||||
:func:`~django.contrib.auth.decorators.login_required` also takes an
|
:func:`~django.contrib.auth.decorators.login_required` also takes an
|
||||||
optional ``redirect_field_name`` parameter. Example::
|
optional ``redirect_field_name`` parameter. Example::
|
||||||
|
|
||||||
from django.contrib.auth.decorators import login_required
|
|
||||||
|
|
||||||
def my_view(request):
|
|
||||||
# ...
|
|
||||||
my_view = login_required(redirect_field_name='redirect_to')(my_view)
|
|
||||||
|
|
||||||
Again, an equivalent example of the more compact decorator syntax
|
|
||||||
introduced in Python 2.4::
|
|
||||||
|
|
||||||
from django.contrib.auth.decorators import login_required
|
from django.contrib.auth.decorators import login_required
|
||||||
|
|
||||||
@login_required(redirect_field_name='redirect_to')
|
@login_required(redirect_field_name='redirect_to')
|
||||||
def my_view(request):
|
def my_view(request):
|
||||||
# ...
|
...
|
||||||
|
|
||||||
:func:`~django.contrib.auth.decorators.login_required` does the following:
|
:func:`~django.contrib.auth.decorators.login_required` does the following:
|
||||||
|
|
||||||
|
@ -1058,23 +1041,15 @@ checks to make sure the user is logged in and has the permission
|
||||||
|
|
||||||
from django.contrib.auth.decorators import user_passes_test
|
from django.contrib.auth.decorators import user_passes_test
|
||||||
|
|
||||||
|
@user_passes_test(lambda u: u.has_perm('polls.can_vote'))
|
||||||
def my_view(request):
|
def my_view(request):
|
||||||
# ...
|
...
|
||||||
my_view = user_passes_test(lambda u: u.has_perm('polls.can_vote'))(my_view)
|
|
||||||
|
|
||||||
We're using this particular test as a relatively simple example. However,
|
We're using this particular test as a relatively simple example. However,
|
||||||
if you just want to test whether a permission is available to a user, you
|
if you just want to test whether a permission is available to a user, you
|
||||||
can use the :func:`~django.contrib.auth.decorators.permission_required()`
|
can use the :func:`~django.contrib.auth.decorators.permission_required()`
|
||||||
decorator, described later in this document.
|
decorator, described later in this document.
|
||||||
|
|
||||||
Here's the same thing, using Python 2.4's decorator syntax::
|
|
||||||
|
|
||||||
from django.contrib.auth.decorators import user_passes_test
|
|
||||||
|
|
||||||
@user_passes_test(lambda u: u.has_perm('polls.can_vote'))
|
|
||||||
def my_view(request):
|
|
||||||
# ...
|
|
||||||
|
|
||||||
:func:`~django.contrib.auth.decorators.user_passes_test` takes a required
|
:func:`~django.contrib.auth.decorators.user_passes_test` takes a required
|
||||||
argument: a callable that takes a
|
argument: a callable that takes a
|
||||||
:class:`~django.contrib.auth.models.User` object and returns ``True`` if
|
:class:`~django.contrib.auth.models.User` object and returns ``True`` if
|
||||||
|
@ -1093,7 +1068,7 @@ checks to make sure the user is logged in and has the permission
|
||||||
|
|
||||||
@user_passes_test(lambda u: u.has_perm('polls.can_vote'), login_url='/login/')
|
@user_passes_test(lambda u: u.has_perm('polls.can_vote'), login_url='/login/')
|
||||||
def my_view(request):
|
def my_view(request):
|
||||||
# ...
|
...
|
||||||
|
|
||||||
The permission_required decorator
|
The permission_required decorator
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
@ -1107,9 +1082,9 @@ The permission_required decorator
|
||||||
|
|
||||||
from django.contrib.auth.decorators import permission_required
|
from django.contrib.auth.decorators import permission_required
|
||||||
|
|
||||||
|
permission_required('polls.can_vote')
|
||||||
def my_view(request):
|
def my_view(request):
|
||||||
# ...
|
...
|
||||||
my_view = permission_required('polls.can_vote')(my_view)
|
|
||||||
|
|
||||||
As for the :meth:`User.has_perm` method, permission names take the form
|
As for the :meth:`User.has_perm` method, permission names take the form
|
||||||
``"<app label>.<permission codename>"`` (i.e. ``polls.can_vote`` for a
|
``"<app label>.<permission codename>"`` (i.e. ``polls.can_vote`` for a
|
||||||
|
@ -1120,9 +1095,9 @@ The permission_required decorator
|
||||||
|
|
||||||
from django.contrib.auth.decorators import permission_required
|
from django.contrib.auth.decorators import permission_required
|
||||||
|
|
||||||
|
permission_required('polls.can_vote', login_url='/loginpage/')
|
||||||
def my_view(request):
|
def my_view(request):
|
||||||
# ...
|
...
|
||||||
my_view = permission_required('polls.can_vote', login_url='/loginpage/')(my_view)
|
|
||||||
|
|
||||||
As in the :func:`~decorators.login_required` decorator, ``login_url``
|
As in the :func:`~decorators.login_required` decorator, ``login_url``
|
||||||
defaults to :setting:`settings.LOGIN_URL <LOGIN_URL>`.
|
defaults to :setting:`settings.LOGIN_URL <LOGIN_URL>`.
|
||||||
|
|
|
@ -332,13 +332,6 @@ to use::
|
||||||
|
|
||||||
from django.views.decorators.cache import cache_page
|
from django.views.decorators.cache import cache_page
|
||||||
|
|
||||||
def my_view(request):
|
|
||||||
...
|
|
||||||
|
|
||||||
my_view = cache_page(my_view, 60 * 15)
|
|
||||||
|
|
||||||
Or, using Python 2.4's decorator syntax::
|
|
||||||
|
|
||||||
@cache_page(60 * 15)
|
@cache_page(60 * 15)
|
||||||
def my_view(request):
|
def my_view(request):
|
||||||
...
|
...
|
||||||
|
@ -365,12 +358,9 @@ requested, subsequent requests to that URL will use the cache.
|
||||||
works in the same way as the ``CACHE_MIDDLEWARE_KEY_PREFIX`` setting for the
|
works in the same way as the ``CACHE_MIDDLEWARE_KEY_PREFIX`` setting for the
|
||||||
middleware. It can be used like this::
|
middleware. It can be used like this::
|
||||||
|
|
||||||
my_view = cache_page(my_view, 60 * 15, key_prefix="site1")
|
|
||||||
|
|
||||||
Or, using Python 2.4's decorator syntax::
|
|
||||||
|
|
||||||
@cache_page(60 * 15, key_prefix="site1")
|
@cache_page(60 * 15, key_prefix="site1")
|
||||||
def my_view(request):
|
def my_view(request):
|
||||||
|
...
|
||||||
|
|
||||||
Specifying per-view cache in the URLconf
|
Specifying per-view cache in the URLconf
|
||||||
----------------------------------------
|
----------------------------------------
|
||||||
|
|
Loading…
Reference in New Issue