首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 更多程式設計知識

Django 1.8+ TEMPLATES 設定完整指南:引擎、載入器、上下文處理器與遷移

     編輯:更多程式設計知識

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引擎專屬參數字典

DjangoTemplates 專屬 OPTIONS


OPTIONS = {
    'context_processors': [...],  # 取代 TEMPLATE_CONTEXT_PROCESSORS
    'debug': True/False,          # 取代 TEMPLATE_DEBUG
    'loaders': [...],             # 取代 TEMPLATE_LOADERS
    'string_if_invalid': '',      # 取代 TEMPLATE_STRING_IF_INVALID
}

舊版設定對應表(1.8 棄用)

  • TEMPLATE_CONTEXT_PROCESSORS → OPTIONS['context_processors']
  • TEMPLATE_DEBUG → OPTIONS['debug']
  • TEMPLATE_DIRS → DIRS
  • TEMPLATE_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。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved