mirror of
https://github.com/django/django.git
synced 2025-10-24 14:16:09 +00:00
magic-removal: Merged to [1774]
git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@1775 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -246,21 +246,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.
|
||||
@@ -268,6 +324,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::
|
||||
@@ -287,9 +362,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
|
||||
-----------------
|
||||
|
||||
|
||||
Reference in New Issue
Block a user