[Django]-Context Processor for specific app only

6👍

The context processor is run for all requests.

If you need to mimic the functionality you speak about, then you could add some if/else conditions in the context processor function, which gets the request object as first argument, so you may determine which app is running and populate the returned dict accordingly

0👍

Most of the answers I’ve found so far are about adding a context_processors into the site’s settings.py. But I want my application to be an independent development unit. On deployment, I don’t want to perform on the site unnecessary adjustments that I could’ve included into the distribution. As it was already hinted above (by Daniel Roseman), we can use custom tags (doc):

# file `extras.py`
from django.template.defaultfilters import register
import platform

CURRENT_OS = platform.system()


@register.simple_tag
def current_os():
    return CURRENT_OS

That can be used like this:

{% load extras %}
. . .
<div>{% current_os %}</div>

As the request object is accessible in a template it may be processed with a custom filter (doc) like this:

# file `extras.py`
@register.filter
def my_filter(req):
    return req.GET.get('q')

Usage:

<div>You searched for '{{ request|my_filter }}'.</div>

Leave a comment