Denis Steinman
Denis Steinman

Reputation: 7799

How to use a custom context processor?

I have written my index action:

def index(request):
    return render_to_response('app/index.html', {
        'title': None,
        'questions': build_questions(),
        'blocks': build_blocks()
    })

But I need to pass an app name for all actions, so I have decided to move it in the context processor:
context_processor.py:

from asknow.settings import APP_NAME


def global_processor(request):
    return {'app_name': APP_NAME}

And in settings.py I connected it to all context processors:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates/')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'asknow.context_processor.global_processor'
            ],
        },
    },
]

But it doesn't work, of course... What am I doing wrong?
P.S. I use Django 1.11.6.

ADDED
Title screenshot
It is a default header, not my site name. In base.html I am trying to print my app_name.

<head>
    <title>
        {{app_name}} {% if title %}&nbsp;-&nbsp;{{title}}{% endif %} 
    </title>
</head>

My index.html extends base.html: {% extends 'common/base.html' %}

Upvotes: 0

Views: 1339

Answers (2)

Arti
Arti

Reputation: 1

You have done almost everything right. You should add a comma after the path to your global_processor in the context_processors in settings.py and it should work.

Upvotes: 0

Denis Steinman
Denis Steinman

Reputation: 7799

It works with using of render only:

def index(request):
    return render(request, 'app/index.html', {
        'title': None,
        'questions': build_questions(),
        'blocks': build_blocks()
    })

Upvotes: 1

Related Questions