1
我在Django項目中使用自己的模板(即/project/app1/template
,/project/app2/template
等)有幾個應用程序。在應用程序模板中啓用上下文處理器
但是,模板上下文處理器(在主應用程序/項目的settings.py
中定義)在這些應用程序模板中不可用。
我必須手動設置context_instance
爲了使兒童模板context處理器(否則背景處理器從模板中丟失):
from django.template import RequestContext
def index(request):
return render_to_response('index.html', {}, context_instance=RequestContext(request))
這裏是我的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",
...
],
},
},
]
如何我是否可以在應用模板中訪問上下文處理器,而無需在每個視圖函數中手動注入context_instance
?