簡體   English   中英

如何使用自定義上下文處理器?

[英]How to use a custom context processor?

我寫了索引操作:

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

但是我需要為所有操作傳遞一個應用程序名稱,因此我決定將其移至上下文處理器中:
context_processor.py

from asknow.settings import APP_NAME


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

並在settings.py中將其連接到所有上下文處理器:

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'
            ],
        },
    },
]

但這當然不起作用...我在做什么錯?
PS我使用Django 1.11.6。

添加
標題截圖
這是默認標頭,而不是我的網站名稱。 base.html中,我嘗試打印我的app_name

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

我的index.html擴展了base.html{% extends 'common/base.html' %}

它僅適用於使用render

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM