Django setting 改进

1548阅读 0评论2011-06-30 eilinx
分类:系统运维

由于自己要在不同的机器上进行开发,而且采用的是CVS控制版本,

有时候主机自己有MySQL环境,有时候又要连接到另外一台数据库服务器,setting里面
数据库主机地址就需要自己动态更改,很不方便,我就在setting上动了一个小手脚,这样
setting文件会根据不同的主机类型自动选择用什么作为DB服务器的地址,这样就不需要
每次手动更改setting文件了.很方便.具体实现如下
  1. def choose_db():
  2.     platform_ = ' '
  3.     
  4.     platform_host = platform.platform()
  5.     platform_ = platform_host[:20]
  6.     
  7.     if platform_ == 'Windows-2008ServerR2':
  8.         db_host = '192.168.1.122'
  9.     else:
  10.         db_host = 'localhost'
  11.     
  12.     return db_host

  13. DB_HOST = choose_db()

  14. DATABASES = {
  15.     'default': {
  16.         'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
  17.         'NAME': 'test', # Or path to database file if using sqlite3.
  18.         'USER': 'test', # Not used with sqlite3.
  19.         'PASSWORD': '123456', # Not used with sqlite3.
  20.         'HOST': DB_HOST, # Set to empty string for localhost. Not used with sqlite3.
  21.         'PORT': '3306', # Set to empty string for default. Not used with sqlite3.
  22.     }
  23. }

上一篇:巧将 Django Admin 应用至前端部分 (ZZ)
下一篇:彻底抛弃了Foxmail