You are hereBlogs > jittat's blog > settings.py สำหรับพัฒนากับสำหรับการ deploy
settings.py สำหรับพัฒนากับสำหรับการ deploy
หลายครั้งเราต้องการกำหนดค่าเริ่มต้นที่แตกต่างกันใน settings.pyสำหรับใช้ในการพัฒนาและในการ deploy ยกตัวอย่างเช่นพวก MEDIA_URL หรือ ADMIN_MEDIA อะไรประมาณนี้
วิธีที่ผมใช้ส่วนมากก็คือสร้าง settings.py ไว้สองอันแยกกัน เวลา deploy ก็อย่าเอาไฟล์นี้ไปทับกัน (อาจทำได้โดยไม่ใส่ไฟล์ดังกล่าวไว้ใน version control system) แต่วิธีนี้มักมีปัญหาว่า บางทีเราแก้ค่าเริ่มต้นบางอย่างที่ส่วนมากจะใช้ร่วมกัน เช่น เพิ่ม APPS เราก็ต้องแก้ทั้งสองไฟล์ แล้วบางทีก็ลืม
วันนี้ไปอ่านอีกวิธีเจอมาใน django user group ใน google groups เลยเอามาเขียนบอกต่อกันครับ
วิธีที่เขาใช้ก็คือเขียน settings_dev.py แยกเอาไว้ แล้วการกำหนดค่าอะไรที่มันแตกต่างก็ใส่เพิ่มไว้ในนั้น เมื่อเรา import โมดูลดังกล่าวเข้ามา พวกการตั้งค่าที่เราเขียนเพิ่มจะไปทับการตั้งค่าเดิม
จากนั้นที่ท้ายไฟล์ settings.py ก็ไปเพิ่มให้ import ไฟล์ดังกล่าวเข้ามา ด้วยโปรแกรมด้านล่าง
from settings_dev import *
except ImportError:
pass
แฟ้มดังกล่าวถ้าเราไม่นำไปใส่ที่จุดที่ deploy พวกการตั้งค่าต่าง ๆ ก็จะเหมือนที่เราตั้งไว้ใน settings.py ปกติครับ
- jittat's blog
- Login or register to post comments

ในการพัฒนาเป็นทีม ที่แต่ละคนมี setting.py เราใช้วิธีสร้างส่วนที่แตกต่างออกไปใน localsetting แล้วค่อย import เข้ามาประมาณนี้
# move local settings for your machine into local_settings.py file in same directory
DEBUG = local.DEBUG
TEMPLATE_DEBUG = local.TEMPLATE_DEBUG
EXTRANET_SERVER = local.EXTRANET_SERVER
EXTRANET_DOWNLOAD_PREFIX = local.EXTRANET_DOWNLOAD_PREFIX
ACCESS_DATABASE_FOLDER = local.ACCESS_DATABASE_FOLDER
ACCESS_DATABASE = local.ACCESS_DATABASE
MYSQLDB_HOST = local.MYSQLDB_HOST
MYSQLDB_USERNAME = local.MYSQLDB_USERNAME
MYSQLDB_PASSWORD = local.MYSQLDB_PASSWORD
MYSQLDB_DATABASE= local.MYSQLDB_DATABASE
ADMINS = (
('xxx Support', 'xxx@xxx.co.uk'),
)
MANAGERS = ADMINS
# 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
DATABASE_ENGINE = hasattr(local, 'DATABASE_ENGINE') and local.DATABASE_ENGINE or 'postgresql_psycopg2'
DATABASE_NAME = local.DATABASE_NAME
DATABASE_HOST = local.DATABASE_HOST
DATABASE_USER = local.DATABASE_USER
DATABASE_PASSWORD = local.DATABASE_PASSWORD
DATABASE_PORT = '' # Set to empty string for default. Not used with sqlite3.
ROOT_URLCONF='xxx.urls'
# 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 = 'Europe/London'
# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-gb'
SITE_ID = 1
# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = False
# 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 = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
# 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',
)
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'xxx.rest.authentication.Authenticate',
)
MIDDLEWARE_CLASSES = (
'yyy.middleware.ForceHTTPS',
'yyy.middleware.DebugOutput',
'yyy.settings.middleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'xxx.rest.authentication.Middleware',
'django.middleware.doc.XViewMiddleware',
'django.middleware.transaction.TransactionMiddleware',
'xxx.core.middleware.CreateContext',
)
TEMPLATE_DIRS = (
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
# Core is before admin so that our admin templates get searched first
'xxx.core',
'Yyy.admin',
'Yyy.actions',
# Front end
'xxx.loginout',
'xxx.intranet',
'xxx.extranet',
'xxx.rest',
# Reports
'xxx.reports',
'xxx.geographic',
# Data model components
'xxx.customer',
'xxx.session',
'xxx.instructor',
'xxx.catalogue',
'xxx.events',
# User applications
'xxx.emailer',
# Data management
'xxx.dbimporters',
#support
)
# Used by django.contrib.auth when it needs to generate a log in URL
LOGIN_URL = r'/extranet/login/'
<blockcode>