简体   繁体   中英

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?
PS I use Django 1.11.6.

ADDED
标题截图
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' %}

It works with using of render only:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM