Django 1.8 引入新的 TEMPLATES 設定結構,取代舊版分散設定。本文詳細說明各選項用途與遷移方式。
TEMPLATES = [{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'APP_DIRS': True,
}]
| 選項 | 說明 |
|---|---|
| BACKEND | 模板引擎類別路徑,內建 DjangoTemplates / Jinja2 |
| NAME | 引擎別名,預設為 BACKEND 最後一段 |
| DIRS | 搜尋模板的目錄列表,按順序 |
| APP_DIRS | 是否搜尋各 app 的 templates 子目錄 |
| OPTIONS | 引擎專屬參數字典 |
OPTIONS = {
'context_processors': [...], # 取代 TEMPLATE_CONTEXT_PROCESSORS
'debug': True/False, # 取代 TEMPLATE_DEBUG
'loaders': [...], # 取代 TEMPLATE_LOADERS
'string_if_invalid': '', # 取代 TEMPLATE_STRING_IF_INVALID
}
TEMPLATE_CONTEXT_PROCESSORS → OPTIONS['context_processors']TEMPLATE_DEBUG → OPTIONS['debug']TEMPLATE_DIRS → DIRSTEMPLATE_LOADERS → OPTIONS['loaders']TEMPLATE_STRING_IF_INVALID → OPTIONS['string_if_invalid']
[
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
]
1.8 後路徑從django.core.context_processors移至django.template.context_processors。