4

我正在关注本教程,以努力在 AWS 上创建一个 Django 应用程序。

http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_Python_django.html

我能够使用本地 sqlite 数据库使一切工作,但我正在尝试将应用程序推送到生产服务器。在执行 Elastic Beanstalk 初始化过程时,我选择创建一个 RDS 实例。

我的 mysite/settings.py 看起来像这样:

导入操作系统

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': os.environ['RDS_DB_NAME'],
        'USER': os.environ['RDS_USERNAME'],
        'PASSWORD': os.environ['RDS_PASSWORD'],
        'HOST': os.environ['RDS_HOSTNAME'],
        'PORT': os.environ['RDS_PORT'],
    }
}

此时我应该可以访问 RDS 参数,但我没有。manage.py 文件变为无响应。

(djangodev)laptop-id:mysite user-name$ python manage.py runserver
Unknown command: 'runserver'
Type 'manage.py help' for usage.

(djangodev)laptop-id:mysite use-name$ python manage.py
Usage: manage.py subcommand [options] [args]

Options:
  -v VERBOSITY, --verbosity=VERBOSITY
                        Verbosity level; 0=minimal output, 1=normal output,
                        2=verbose output, 3=very verbose output
  --settings=SETTINGS   The Python path to a settings module, e.g.
                        "myproject.settings.main". If this isn't provided, the
                        DJANGO_SETTINGS_MODULE environment variable will be
                        used.
  --pythonpath=PYTHONPATH
                        A directory to add to the Python path, e.g.
                        "/home/djangoprojects/myproject".
  --traceback           Print traceback on exception
  --version             show program's version number and exit
  -h, --help            show this help message and exit
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 453, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 376, in execute
    sys.stdout.write(self.main_help_text() + '\n')
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 242, in main_help_text
    for name, app in six.iteritems(get_commands()):
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 109, in get_commands
    apps = settings.INSTALLED_APPS
  File "/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in _setup
    self._wrapped = Settings(settings_module)
  File "/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 132, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/Users/andrewcopp/Developer/mysite/mysite/settings.py", line 17, in <module>
   'NAME': os.environ['RDS_DB_NAME'],                      # Or path to database file if using sqlite3.
  File "/tmp/djangodev/bin/../lib/python2.7/UserDict.py", line 23, in __getitem__
    raise KeyError(key)
KeyError: 'RDS_DB_NAME'

关于我可能忽略的任何想法?我可以提供任何更多信息以使事情变得清晰

4

1 回答 1

0

您需要在设置中本地回退到不同的数据库。

在您的settings.py文件中,将 DATABASE 变量替换为:

DATABASES = {}

try:
    from local_settings import *
except ImportError, e:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': os.environ['RDS_DB_NAME'],
            'USER': os.environ['RDS_USERNAME'],
            'PASSWORD': os.environ['
            'HOST': os.environ['RDS_HOSTNAME'],
            'PORT': os.environ['RDS_PORT'],
        }
    }

现在在与您的 settings.py 相同的目录中创建一个local_settings.py并输入以下代码:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'db.djangodb',
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    }
}

MEDIA_ROOT = ''
MEDIA_URL = ''
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = ()
TEMPLATE_DIRS = ()

现在将 local_settings.py 文件添加到.gitignore文件中。

运行$ python manage.py syncdb,现在您可以在本地运行 django 服务器了。

其中大部分是我发现的这篇博文中的意大利面:http: //grigory.ca/2012/09/getting-started-with-django-on-aws-elastic-beanstalk/

于 2013-11-20T16:19:07.967 回答