Source

django-template-utils / docs / context_processors.txt

The default branch has multiple heads

===============================
Context processors for settings
===============================


It's often useful for an application to expose the values of certain
settings -- especially custom settings -- for use in
templates. Django's ``RequestContext`` and context processors provide
an easy way to automatically add certain variables to every template
context, but (for good reasons) Django does not provide a context
processor which can expose the values of settings.

This module provides a simple function which can generate context
processors for settings, and a pre-generated context processor for the
most common use case: media settings.


``template_utils.context_processors.settings_processor``
========================================================

This function takes a list of setting names, and returns a new
function: a context processor which will add the values of those
settings to any ``RequestContext`` in which it is used.

So, for example, you might define a file ``context_processors.py`` in
your application, and place the following in it::

    from template_utils.context_processors import settings_processor
    my_settings_processor = settings_processor('INTERNAL_IPS', 'SITE_ID')

The function ``my_settings_processor`` would be a context processor
which added the values of the settings ``INTERNAL_IPS`` and
``SITE_ID`` to each ``RequestContext`` in which it was used, and then
you would just need to add
``myproject.myapp.context_processors.my_settings_processor`` (changing
the name to suit the actual names of your project and application, of
course) to your `` TEMPLATE_CONTEXT_PROCESSORS`` setting.


``template_utils.context_processors.media``
===========================================

This is a pre-generated context processor which adds the values of the
settings ``MEDIA_URL`` and ``ADMIN_MEDIA_PREFIX`` to each
``RequestContext`` in which it is used.

To enable it, add ``template_utils.context_processors.media`` to your
``TEMPLATE_CONTEXT_PROCESSORS`` setting.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.