django-storages - 静态文件存储键属性错误

问题描述 投票:0回答:1

我正在尝试使用 django-stores 配置 Django (5.0.4) 以使用 Google Cloud S3 作为我的文件存储。

这是我的对象存储的 settings.py 配置:

from google.oauth2.service_account import Credentials

STORAGES = {
    "default": {
        "BACKEND": "storages.backends.gcloud.GoogleCloudStorage",
        "OPTIONS": {
            "GS_BUCKET_NAME": "BUCKET",
            "GS_PROJECT_ID": "PROJECT",
            "GS_CREDENTIALS": Credentials.from_service_account_file("service-account.json"),
        },
    },
    "staticfiles": "storages.backends.gcloud.GoogleCloudStorage"
}

运行后

python manage.py collectstatic
我收到以下属性错误

Traceback (most recent call last):
  File "/home/victor/BmLabs/site/manage.py", line 22, in <module>
    main()
  File "/home/victor/BmLabs/site/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/home/victor/.pyenv/versions/bmlabs/lib/python3.12/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/home/victor/.pyenv/versions/bmlabs/lib/python3.12/site-packages/django/core/management/__init__.py", line 382, in execute
    settings.INSTALLED_APPS
  File "/home/victor/.pyenv/versions/bmlabs/lib/python3.12/site-packages/django/conf/__init__.py", line 89, in __getattr__
    self._setup(name)
  File "/home/victor/.pyenv/versions/bmlabs/lib/python3.12/site-packages/django/conf/__init__.py", line 76, in _setup
    self._wrapped = Settings(settings_module)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/victor/.pyenv/versions/bmlabs/lib/python3.12/site-packages/django/conf/__init__.py", line 262, in __init__
    self.STORAGES.get(STATICFILES_STORAGE_ALIAS, {}).get("BACKEND"),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'get'

根据 指南,我已将

staticfiles
键设置为
storages.backends.gcloud.GoogleCloudStorage

django google-cloud-storage
1个回答
2
投票

old Django 版本中,有独立的顶级变量

DEFAULT_FILE_STORAGE
和/或
STATICFILES_STORAGE
,它们接受单个字符串。

在现代 Django 版本中,有

STORAGES
,它需要带有
BACKEND
键的字典字典。使
staticfiles
存储配置看起来就像您使
default
配置看起来一样,而不仅仅是单个字符串。

© www.soinside.com 2019 - 2024. All rights reserved.