如何解决错误:collectstatic - 找不到后端“storages.custom_azure.AzureStaticStorage”

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

我有一个 django 应用程序,并且我已经安装了模块:

django-storages[azure]
,命令为:
pipenv install django-storages[azure]

现在我尝试运行collectstatic命令。但如果我输入命令:

python manage.py collectstatic

我收到此错误:

Traceback (most recent call last):
  File "C:\Users\engel\.virtualenvs\django_upload-sYcQHG5j\lib\site-packages\django\core\files\storage\handler.py", line 52, in create_storage
    storage_cls = import_string(backend)
  File "C:\Users\engel\.virtualenvs\django_upload-sYcQHG5j\lib\site-packages\django\utils\module_loading.py", line 30, in import_string
    return cached_import(module_path, class_name)
  File "C:\Users\engel\.virtualenvs\django_upload-sYcQHG5j\lib\site-packages\django\utils\module_loading.py", line 15, in cached_import
    module = import_module(module_path)
  File "C:\Python310\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'storages.custom_azure'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\repos\django_upload\myuploader\manage.py", line 22, in <module>
    main()
  File "C:\repos\django_upload\myuploader\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\engel\.virtualenvs\django_upload-sYcQHG5j\lib\site-packages\django\core\management\__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "C:\Users\engel\.virtualenvs\django_upload-sYcQHG5j\lib\site-packages\django\core\management\__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\engel\.virtualenvs\django_upload-sYcQHG5j\lib\site-packages\django\core\management\base.py", line 412, in run_from_argv
    self.execute(*args, **cmd_options)
    output = self.handle(*args, **options)
  File "C:\Users\engel\.virtualenvs\django_upload-sYcQHG5j\lib\site-packages\django\contrib\staticfiles\management\commands\collectstatic.py", line 184, in handle   
    if self.is_local_storage() and self.storage.location:
  File "C:\Users\engel\.virtualenvs\django_upload-sYcQHG5j\lib\site-packages\django\contrib\staticfiles\management\commands\collectstatic.py", line 245, in is_local_storage
    return isinstance(self.storage, FileSystemStorage)
  File "C:\Users\engel\.virtualenvs\django_upload-sYcQHG5j\lib\site-packages\django\utils\functional.py", line 280, in __getattribute__
    value = super().__getattribute__(name)
  File "C:\Users\engel\.virtualenvs\django_upload-sYcQHG5j\lib\site-packages\django\utils\functional.py", line 251, in inner
    self._setup()
  File "C:\Users\engel\.virtualenvs\django_upload-sYcQHG5j\lib\site-packages\django\contrib\staticfiles\storage.py", line 540, in _setup
    self._wrapped = storages[STATICFILES_STORAGE_ALIAS]
  File "C:\Users\engel\.virtualenvs\django_upload-sYcQHG5j\lib\site-packages\django\core\files\storage\handler.py", line 43, in __getitem__
    storage = self.create_storage(params)
  File "C:\Users\engel\.virtualenvs\django_upload-sYcQHG5j\lib\site-packages\django\core\files\storage\handler.py", line 54, in create_storage
    raise InvalidStorageError(f"Could not find backend {backend!r}: {e}") from e
django.core.files.storage.handler.InvalidStorageError: Could not find backend 'storages.custom_azure.AzureStaticStorage': No module named 'storages.custom_azure'PS C

所以我的 pipfile 看起来:

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
django = "*"
django-storages = {extras = ["azure"], version = "*"}

[dev-packages]

[requires]
python_version = "3.10"

settings.py 文件:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    "uploader",
    "storages"
]

STATIC_URL = '/static/'
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
DEFAULT_FILE_STORAGE = 'uploader.custom_azure.AzureMediaStorage'
STATICFILES_STORAGE = 'storages.custom_azure.AzureStaticStorage'
STATIC_LOCATION = "static"
MEDIA_LOCATION = "media"
AZURE_ACCOUNT_NAME = "name"
AZURE_CUSTOM_DOMAIN = f'{AZURE_ACCOUNT_NAME}.blob.core.windows.net'
STATIC_URL = f'https://{AZURE_CUSTOM_DOMAIN}/{STATIC_LOCATION}/'
MEDIA_URL = f'https://{AZURE_CUSTOM_DOMAIN}/{MEDIA_LOCATION}/'

我在应用程序上传器中有一个文件 custom.azure.py:

from storages.backends.azure_storage import AzureStorage

class AzureMediaStorage(AzureStorage):   
    account_name = ''name'
    # Must be replaced by your <storage_account_key>
    account_key= 'key'
    azure_container = 'media'
    expiration_secs = None


class AzureStaticStorage(AzureStorage):   
    account_name = 'name'   
    account_key = 'key'
    azure_container = 'static'
    expiration_secs = None

问题:如何解决错误:

Could not find backend 'storages.custom_azure.AzureStaticStorage': No module named 'storages.custom_azure' 

python django azure azure-blob-storage
1个回答
0
投票

好的,解决了:

DEFAULT_FILE_STORAGE = 'storages.backends.azure_storage.AzureStorage'

我删除了这个:

STATICFILES_STORAGE = 'storages.custom_azure.AzureStaticStorage'

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