我想将我的Django网络应用程序数据库连接到我的postgresql数据库我已经在我的Pythonanywhere付费帐户上。在编写任何代码之前,我只是想让所有的事情都彼此交谈。这是我的django应用程序中的settings.py DATABASE部分。我正在运行Python 3.5和Django 1.9。将Django的Web应用程序数据库连接到Pythonanywhere上的postgresql
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': '[myDatabaseName]',
'USER': '[myUsername]',
'PASSWORD': '[myPassword]',
'HOST': 'xxxxxxxx-xxx.postgres.pythonanywhere-services.com',
'PORT': '10130',
}
}
HOST和PORT我们都是从pythonanywhere.com网站的DATABASE和Postgres标签下提供的。我确实在postgres控制台上创建了我的数据库,用户名和密码。
然后我创建了一个checkedb.py脚本,我发现它会检查与postgres数据库的连接是否工作。
from django.db import connections
db_conn = connections['default']
try:
c = db_conn.cursor()
except OperationalError:
connected = False
else:
connected = True
这是运行此代码后收到的错误。
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/django/conf/__init__.py", line 38, in _setup
settings_module = os.environ[ENVIRONMENT_VARIABLE]
File "/usr/lib/python3.4/os.py", line 633, in __getitem__
raise KeyError(key) from None
KeyError: 'DJANGO_SETTINGS_MODULE'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/giraldez/golf/golf/dbcheck.py", line 2, in <module>
db_conn = connections['default']
File "/usr/local/lib/python3.4/dist-packages/django/db/utils.py", line 196, in __getitem__
self.ensure_defaults(alias)
File "/usr/local/lib/python3.4/dist-packages/django/db/utils.py", line 170, in ensure_defaults
conn = self.databases[alias]
File "/usr/local/lib/python3.4/dist-packages/django/utils/functional.py", line 49, in __get__
res = instance.__dict__[self.func.__name__] = self.func(instance)
File "/usr/local/lib/python3.4/dist-packages/django/db/utils.py", line 153, in databases
self._databases = settings.DATABASES
File "/usr/local/lib/python3.4/dist-packages/django/conf/__init__.py", line 54, in __getattr__
self._setup(name)
File "/usr/local/lib/python3.4/dist-packages/django/conf/__init__.py", line 47, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable D
JANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
为我的项目目录看起来像这样
golf/
---golf/
------__init.py__
------dbcheck.py
------settings.py
------urls.py
------wsgi.py
---media/
---static/
---manage.py
您是否已检查安装的所有应用程序都包含在INSTALLED_APPS sttings变量中? – 2016-04-25 05:37:11