gpt4 book ai didi

python - 将 Django Web 应用程序数据库连接到 Pythonanywhere 上的 postgresql

转载 作者:搜寻专家 更新时间:2023-10-30 20:16:28 25 4
gpt4 key购买 nike

我想将我的 Django 网络应用程序数据库连接到我的 Pythonanywhere 付费帐户中的 postgresql 数据库。在编写任何代码之前,我只想让所有东西相互交谈。这是我的 django 应用程序中的 settings.py DATABASE 部分。我正在运行 Python 3.5 和 Django 1.9。

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

最佳答案

如果您将 django 用作独立脚本,则需要先设置它。尝试使用 ./manage.py shell 会更容易。但如果您想使用独立脚本进行测试,请执行以下操作:

import sys,os

if __name__ == '__main__': # pragma nocover
# Setup environ
sys.path.append(os.getcwd())

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "main.settings_dev")
import django
django.setup()

from django.db import connections
db_conn = connections['default']
try:
c = db_conn.cursor()
except OperationalError:
connected = False
else:
connected = True

关于python - 将 Django Web 应用程序数据库连接到 Pythonanywhere 上的 postgresql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36831274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com