diff options
Diffstat (limited to 'docs/templates_python.txt')
| -rw-r--r-- | docs/templates_python.txt | 90 |
1 files changed, 81 insertions, 9 deletions
diff --git a/docs/templates_python.txt b/docs/templates_python.txt index de212cd141..11ff5919c5 100644 --- a/docs/templates_python.txt +++ b/docs/templates_python.txt @@ -240,21 +240,77 @@ Subclassing Context: DjangoContext Django comes with a special ``Context`` class, ``django.core.extensions.DjangoContext``, that acts slightly differently than -the normal ``django.core.template.Context``. It takes an ``HttpRequest`` object -as its first argument, and it automatically populates the context with a few -variables: +the normal ``django.core.template.Context``. The first difference is that takes +an `HttpRequest object`_ as its first argument. For example:: + + c = DjangoContext(request, { + 'foo': 'bar', + } + +The second difference is that it automatically populates the context with a few +variables, according to your `TEMPLATE_CONTEXT_PROCESSORS setting`_. + +The ``TEMPLATE_CONTEXT_PROCESSORS`` setting is a tuple of callables that take a +request object as their argument and return a dictionary of items to be merged +into the context. By default, ``TEMPLATE_CONTEXT_PROCESSORS`` is set to:: + + ("django.core.context_processors.auth", + "django.core.context_processors.debug", + "django.core.context_processors.i18n") + +Each processor is applied in order. That means, if one processor adds a +variable to the context and a second processor adds a variable with the same +name, the second will override the first. The default processors are explained +below. + +Also, you can give ``DjangoContext`` a list of additional processors, using the +optional, third positional argument, ``processors``. In this example, the +``DjangoContext`` instance gets a ``ip_address`` variable:: + + def ip_address_processor(request): + return {'ip_address': request.META['REMOTE_ADDR']} + + def some_view(request): + # ... + return DjangoContext({ + 'foo': 'bar', + }, [ip_address_processor]) + +Note: The concept of template-context processors is new in the Django +development version. In Django 0.90, ``DjangoContext`` automatically populates +the context with all of the values explained below, but it's not possible to +add and remove processors. + +Here's what each of the default processors does: + +.. _HttpRequest object: http://www.djangoproject.com/documentation/request_response/#httprequest-objects +.. _TEMPLATE_CONTEXT_PROCESSORS setting: http://www.djangoproject.com/documentation/settings/#template-context_processors + +django.core.context_processors.auth +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +If ``TEMPLATE_CONTEXT_PROCESSORS`` contains this processor, every +``DjangoContext`` will contain these three variables: * ``user`` -- An ``auth.User`` instance representing the currently logged-in user (or an ``AnonymousUser`` instance, if the client isn't logged in). See the `user authentication docs`. * ``messages`` -- A list of ``auth.Message`` objects for the currently logged-in user. - * ``perms`` -- An instance of ``django.core.extensions.PermWrapper``, + * ``perms`` -- An instance of ``django.core.context_processors.PermWrapper``, representing the permissions that the currently logged-in user has. See the `permissions docs`_. -Also, if your ``DEBUG`` setting is set to ``True``, every ``DjangoContext`` -instance has the following two extra variables: +.. _user authentication docs: http://www.djangoproject.com/documentation/models/authentication/#users +.. _permissions docs: http://www.djangoproject.com/documentation/models/authentication/#permissions + +django.core.context_processors.debug +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +If ``TEMPLATE_CONTEXT_PROCESSORS`` contains this processor, every +``DjangoContext`` will contain these two variables -- but only if your +``DEBUG`` setting is set to ``True`` and the request's IP address +(``request.META['REMOTE_ADDR']``) is in the ``INTERNAL_IPS`` setting: * ``debug`` -- ``True``. You can use this in templates to test whether you're in ``DEBUG`` mode. @@ -262,6 +318,25 @@ instance has the following two extra variables: representing every SQL query that has happened so far during the request and how long it took. The list is in order by query. +django.core.context_processors.i18n +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +If ``TEMPLATE_CONTEXT_PROCESSORS`` contains this processor, every +``DjangoContext`` will contain these two variables: + + * ``LANGUAGES`` -- The value of the `LANGUAGES setting`_. + * ``LANGUAGE_CODE`` -- ``request.LANGUAGE_CODE``, if it exists. Otherwise, + the value of the `LANGUAGE_CODE setting`_. + +See the `internationalization docs`_ for more. + +.. _LANGUAGES setting: http://www.djangoproject.com/documentation/settings/#languages +.. _LANGUAGE_CODE setting: http://www.djangoproject.com/documentation/settings/#language-code +.. _internationalization docs: http://www.djangoproject.com/documentation/i18n/ + +Subclassing Context: Custom subclasses +-------------------------------------- + Feel free to subclass ``Context`` yourself if you find yourself wanting to give each template something "automatically." For instance, if you want to give every template automatic access to the current time, use something like this:: @@ -281,9 +356,6 @@ This technique has two caveats: * You'll have to be careful not to set the variable ``current_time`` when you populate this context. If you do, you'll override the other one. -.. _user authentication docs: http://www.djangoproject.com/documentation/models/authentication/#users -.. _permissions docs: http://www.djangoproject.com/documentation/models/authentication/#permissions - Loading templates ----------------- |
