导入存储区时出错

导入存储区时出错

本文介绍了Django - 导入存储区时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个自定义存储后端,该文件称为 storages.py ,并被放置在名为 core

I have created a custom storage backend, the file is called storages.py and is placed in an app called core:

from django.conf import settings
from storages.backends.s3boto import S3BotoStorage

class S3StaticBucket(S3BotoStorage):
    def __init__(self, *args, **kwargs):
        kwargs['bucket_name'] = getattr(settings, 'static.mysite.com')
        super(S3BotoStorage, self).__init__(*args, **kwargs)

code> settings.py ,我有以下:

In settings.py, I have the follwing:

STATICFILES_STORAGE = 'core.storages.S3StaticBucket'
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'

当我尝试做 python manage.py collectstatic 它显示以下错误:

When I try to do python manage.py collectstatic it shows the following error:

django.core.exceptions.ImproperlyConfigured: Error importing storage module core.storages: "No module named backends.s3boto"

当我运行 python manage.py shell 并尝试导入相同:

And when I run python manage.py shell and try to import the same:

>>>
>>> from django.conf import settings
>>> from storages.backends.s3boto import S3BotoStorage
>>>

任何想法我在做错什么?

Any idea what I'm doing wrong?

推荐答案

有一个命名空间冲突; 存储绝对名称与存储本地名称冲突。它可能不直观,但您可以从模块本身导入:

There is a namespace conflict; the storage absolute name clashes with a storage local name. It may be unintuitive, but you can import from module in itself:

// file my_module/clash.py
import clash
print clash.__file__

现在我们在一个包含一个 my_module

Now we run pyhon shell in a dir containing a my_module:

$ python
>>> import my_module.clash
my_module.clash.py

简而言之,您的模块尝试导入

In short, your module tries to import a backend from itself.

您需要绝对导入 - 。

You need an absolute import - Trying to import module with the same name as a built-in module causes an import error.

这篇关于Django - 导入存储区时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 16:47