Collectstatic返回由于dj_database_url()引起的KeyError

时间:2018-03-27 09:52:30

标签: django postgresql django-models postgis

我正在实施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

根据我的理解,似乎期待一个引擎,这是一个方案(?),如&#34; sqlite&#34;,或&#34; postgis&#34;。我相信我正在设置合适的引擎。是否需要在其他任何地方设置?

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

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

也无济于事。

1 个答案:

答案 0 :(得分:1)

错误是具体的。您正尝试使用空字符串作为键来访问字典(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 

(取代当然的正确值)。它应该在这样运行时起作用。