gpt4 book ai didi

django - 由于 dj_database_url(),Collectstatic 返回 KeyError

转载 作者:行者123 更新时间:2023-11-29 12:53:05 25 4
gpt4 key购买 nike

我正在实现 dj_database_url 但在运行 collectstatic 时收到错误。

dj_database_url readme之后,以及 Heroku 概述的步骤 here ,我将其添加到我的 settings.py 的底部:

import dj_database_url

DATABASES['default'] = dj_database_url.config(default='postgis://USER:PASSWORD@HOST:PORT/NAME')
DATABASES['default']['ENGINE'] = 'django.contrib.gis.db.backends.postgis'

GDAL_LIBRARY_PATH = os.getenv('GDAL_LIBRARY_PATH')
GEOS_LIBRARY_PATH = os.getenv('GEOS_LIBRARY_PATH')

添加后,collectstatic 会给我这个回溯:

Traceback (most recent call last):
File "./manage.py", line 29, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 308, in execute
settings.INSTALLED_APPS
File "/usr/local/lib/python3.6/site-packages/django/conf/__init__.py", line 56, in __getattr__
self._setup(name)
File "/usr/local/lib/python3.6/site-packages/django/conf/__init__.py", line 41, in _setup
self._wrapped = Settings(settings_module)
File "/usr/local/lib/python3.6/site-packages/django/conf/__init__.py", line 110, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/local/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/usr/src/app/config/settings/production.py", line 211, in <module>
DATABASES['default'] = dj_database_url.config(default='postgis://USER:PASSWORD@HOST:PORT/NAME')
File "/usr/local/lib/python3.6/site-packages/dj_database_url.py", line 55, in config
config = parse(s, engine, conn_max_age, ssl_require)
File "/usr/local/lib/python3.6/site-packages/dj_database_url.py", line 103, in parse
engine = SCHEMES[url.scheme] if engine is None else engine
KeyError: ''

关于这个问题的文档很少。我确实发现了这个 github 问题:https://github.com/vitorfs/bootcamp/issues/78

这建议我编辑我的 ALLOWED_HOSTS。我已经尝试过了,但没有用。

这是回溯底部引用的代码行:
https://github.com/kennethreitz/dj-database-url/blob/master/dj_database_url.py#L103

根据我的理解,它似乎需要一个引擎,它是一个方案(?),例如“sqlite”或“postgis”。我确实相信我正在设置合适的引擎。是否需要在其他地方设置?

我尝试将代码顺序反转为:

...
DATABASES['default']['ENGINE'] = 'django.contrib.gis.db.backends.postgis'
DATABASES['default'] = dj_database_url.config(default='postgis://USER:PASSWORD@HOST:PORT/NAME')

也无济于事。

最佳答案

错误是特定的。您正在尝试使用空字符串作为键访问字典 (SCHEMES)(因此 url.scheme 的值是 '')。现在,dj_database_url 将首先尝试从 DATABASE_URL 环境值中获取 URL,如果找不到此变量,则将使用默认值。从您得到的错误中可以明显看出默认值并未实际使用,因此它会尝试从 DATABASE_URL 环境变量中解析 url。您如何运行 collectstatic 管理命令?在运行之前尝试显式设置 DATABASE_URL。例如,打开一个 bash shell 并运行如下命令

DATABASE_URL=postgis://USER:PASSWORD@HOST:PORT/NAME python manage.py collectstatic 

(当然是替换正确的值)。它应该像这样运行。

关于django - 由于 dj_database_url(),Collectstatic 返回 KeyError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49509711/

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