settings.py
1. 修改時區:
默認為:TIME_ZONE = 'America/Chicago'
修改為:TIME_ZONE = 'Asia/Shanghai'
2. 默認的url:
ROOT_URLCONF = 'mysite.urls'
3. 模板位置:
TEMPLATE_DIRS = (.....)
- import os.path
- TEMPLATE_DIRS = (
- #'/home/tony/djcode/mysite/templates',
- os.path.join(os.path.dirname(__file__),'templates').replace('\\','/'),
- )
4. 數據庫配置:(MySQL)
- DATABASES = {
- 'default': {
- 'ENGINE': 'mysql',
- 'NAME': 'projectforge',
- 'USER': 'root',
- 'PASSWORD': 'root',
- 'HOST': 'localhost',
- 'PORT': '5432'
- }
- }
============================================================================================
文章來源:http://club.topsage.com/thread-2261820-1-1.html
Django訪問數據庫的設置是在settings.py中寫入數據庫的engine、用戶名和密碼,默認的寫法是:
DATABASE_ENGINE = 'mysql' # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
DATABASE_NAME = 'xxx' # Or path to database file if using sqlite3.
DATABASE_USER = 'root' # Not used with sqlite3.
DATABASE_PASSWORD = 'xxx' # Not used with sqlite3.
DATABASE_HOST = 'localhost' # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = '3306' # Set to empty string for default. Not used with sqlite3.
數據庫的關鍵信息都寫在settings.py中,這樣做是非常不安全的。現在可以在settings.py里面使用DATABASE項代替以上的配置項,username和password可以寫在配置文件中。下面是把username和password放到MySQL數據庫的配置文件中,由 DATABASE項讀取的示例:
- DATABASES = {
- 'default': {
- 'ENGINE': 'django.db.backends.mysql',
- 'OPTIONS': {
- 'read_default_file': '/etc/mysql/my.cnf',
- },
- }
- }
- # my.cnf
- [client]
- database = xxx
- user = xxx
- password = xxxxxx
- default-character-set = utf8
也可以在DATABASES中加入NAME來指定數據庫名,client中去除database選項,HOST和PORT這些也都可以寫在my.cnf文件中。