0

I have 2 databases of interest, the basic one and the development one:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| projectsdb         |
| projectsdb_dev     |
+--------------------+
3 rows in set (0.00 sec)

In my django file mysite/mysite/settings.py, my databases are declared this way:

DATABASES = {  
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'projectsdb',
        'USER': 'projectsdb',
        'PASSWORD': 'notsecure',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }
}

The allowed hosts is:

ALLOWED_HOSTS = ['xxx.xx.xxx.xx']  # I replaced it for the example

I start the server on the port 8006 which I use for developing:

$ python ./manage.py runserver xxx.xx.xxx.xx:8006

And here I modify the production database. I can switch to the dev database replacing the default database name:

DATABASES = {  
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'projectsdb_dev',
        'USER': 'projectsdb',
        'PASSWORD': 'notsecure',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }
}

And it works fine, the server is interacting with the projectsdb_dev database. However I would like to keep both databases available in the settings file and I saw tutorials setting it up this way:

DATABASES = { 
    'default': {}, 
    'prod': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'projectsdb',
        'USER': 'projectsdb',
        'PASSWORD': 'notsecure',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    },  
    'dev': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'projectsdb_dev',
        'USER': 'projectsdb',
        'PASSWORD': 'notsecure',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }   
}

Now when I open the web page on xxx.xx.xxx.xx:8006, I get this error:

ImproperlyConfigured at /admin/

settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.

I don't know if it's relevant but I also have this table:

mysql> select * from django_site;
+----+--------------------+----------------+
| id | domain             | name           |
+----+--------------------+----------------+
|  1 | example.com        | example.com    |
|  2 | xxx.xx.xxx.xx:8000 | projectsdb     |
|  3 | xxx.xx.xxx.xx:8006 | projectsdb_dev |
+----+--------------------+----------------+
3 rows in set (0.00 sec)

How can I run the server specifying the correct database I want?

4

2 回答 2

4

我想说,为dev和创建单独的设置文件prod。或者对于只有数据库的情况,您可以使用环境变量。

$ export ENV=PROD

然后在settings.py

import os
if os.environ.get('ENV') == "PROD":
    DATABASES = { 
      'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'projectsdb',
        'USER': 'projectsdb',
        'PASSWORD': 'notsecure',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
        }, 
    }
else:
    DATABASES = { 
      'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'projectsdb_dev',
        'USER': 'projectsdb',
        'PASSWORD': 'notsecure',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
        },
    } 
于 2017-11-30T17:56:36.067 回答
1

当您不指定默认数据库时,您需要使用自动数据库路由器来指定您的模型将使用哪个数据库。您将编写这些路由器类,然后指定 DATABASE_ROUTERS 设置以指向它们。路由器基本上包含一些用于告诉数据库操作去哪里的逻辑,您可以检查这是否是一个开发环境。

于 2017-11-30T18:00:45.940 回答