Django網頁框架若要正常運作,設定值(setting)必須正確。設定主要是由settings.py這個檔案所設置,檔案內容將包含一些指令,這些指令會告訴Django要做什麼事。
本文將介紹設定的一些觀念,先看看Django預設產生的settings.py有什麼東西(請見下方程式碼的部分),這樣你會更清楚發生了什麼事情。
首先是除錯(debug)的設定,DEBUG=True開啟除錯模式,而TEMPLATE_DEBUG=DEBUG則是開啟除錯模式的樣版。
接著是系統管理員(administrator)相關的設定。ADMINS是設定接收錯誤訊息通知(code error notification)的信箱,預設為空值,可以設定多人。MANAGERS則是設定無效連結通知(broken-link notification)的信箱,這裡指定與ADMINS相同的空tuple值。
資料庫相關設定,有DATABASE_ENGINE、DATABASE_NAME、DATABASE_USER、DATABASE_PASSWORD、DATABASE_HOST與DATABASE_PORT。
國際化相關設定,TIME_ZONE設定時區,LANGUAGE_CODE設定地區語系。例如TIME_ZONE='Asia/Taipei',以及LANGUAGE_CODE='zh-tw'。另外,USE_I18N表示是否啟用系統的國際化機制。
SITE_ID是用在多網站系統,參閱官網The “sites” framework說明。
媒體相關設定:MEDIA_ROOT與ADMIN_MEDIA_PREFIX,用來設定放置靜態檔案(不是python程式碼)的路徑。MEDIA_URL則是媒體對應的網址路徑。
SECRET_KEY設定安全鑰匙的字串。
TEMPLATE_LOADERS設定樣版的載入器。
MIDDLEWARE_CLASSES設定中介軟體(Middleware)使用的類別模組。
ROOT_URLCONF是設定url對應檔的模組路徑名稱,一般是要設定成urls.py的模組檔案。
TEMPLATE_DIRS設定樣版檔案所在路徑。
INSTALLED_APPS設定安裝的套件。
還有其他設定參數,請參閱設定值清單。
# Django settings for WebApp project.
DEBUG = True
TEMPLATE_DEBUG = DEBUG
ADMINS = (
# ('Your Name', 'your_email@domain.com'),
)
MANAGERS = ADMINS
DATABASE_ENGINE = '' # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
DATABASE_NAME = '' # Or path to database file if using sqlite3.
DATABASE_USER = '' # Not used with sqlite3.
DATABASE_PASSWORD = '' # Not used with sqlite3.
DATABASE_HOST = '' # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = '' # Set to empty string for default. Not used with sqlite3.
# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'America/Chicago'
# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'
SITE_ID = 1
# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True
# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = ''
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = ''
# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/media/'
# Make this unique, and don't share it with anybody.
SECRET_KEY = '+r+_06y3g^)8+ixwki00v5&8v4213*q2)4e#a)1npm59cdvc1d'
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.load_template_source',
'django.template.loaders.app_directories.load_template_source',
# 'django.template.loaders.eggs.load_template_source',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
)
ROOT_URLCONF = 'WebApp.urls'
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
)