lundi 13 juin 2016

Enabling context processors in app templates

I have a few apps in a Django project with their own templates (i.e. /project/app1/template, /project/app2/template and so on).

However, template context processors (which are defined in the settings.py of the main app/project) are not available in those app templates.

I have to manually set the context_instance in order to enable context processors in child templates (otherwise context processors are missing from the templates):

from django.template import RequestContext

def index(request):
    return render_to_response('index.html', {}, context_instance=RequestContext(request))

Here's my settings.py:

PACKAGE_ROOT = os.path.abspath(os.path.dirname(__file__))
BASE_DIR = PACKAGE_ROOT


TEMPLATES = [
    {
       "DIRS": [
            os.path.join(PACKAGE_ROOT, "templates"),
        ],
        "APP_DIRS": True,

        "OPTIONS": {
            ...
            "context_processors": [
                "django.contrib.auth.context_processors.auth",
                "django.template.context_processors.request",
                ...
            ],
        },
    },
]

How do I access context processors in the app templates without manually injecting context_instance in each view function?

Aucun commentaire:

Enregistrer un commentaire