我已经写了我的索引操作:
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'
],
},
},
]
但这当然行不通……我做错了什么?
P.S. 我使用 Django 1.11.6。
已添加
这是默认标题,而不是我的网站名称。在 base.html 中,我正在尝试打印我的
app_name
。
<head>
<title>
{{app_name}} {% if title %} - {{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()
})
你几乎所有事情都做对了。您应该在 settings.py 的 context_processors 中的 global_processor 路径后面添加一个逗号,它应该可以工作。