如何在dropbox上使用django-storage进行媒体存储?

时间:2018-04-08 08:09:58

标签: python django dropbox django-storage

我正在使用django-storagesDropbox上投放媒体文件。但我无法让它工作(mdeia文件仍然存储在本地服务器中)。

我已经安装了dropbox和django-storage,并使用permission type:app folder创建了一个应用,然后添加了相关设置。

这是我的设置:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

DEBUG = False

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_filters',
    'website',
    'storages',
)

# for dropbox
DEFAULT_FILE_STORAGE = 'storages.backends.dropbox.DropBoxStorage'
DROPBOX_OAUTH2_TOKEN = 'my_generated_token_from_dropbox'
DROPBOX_ROOT_PATH = 'media'


MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

为了能够将媒体文件上传到Dropbox(并阅读它们),必须在设置代码中更改哪些内容?

由于

1 个答案:

答案 0 :(得分:1)

好的,发现了问题!设置是正确的,问题是由于某种原因我使用自定义存储FileField!我所要做的就是删除storage=...

file = models.FileField(upload_to=some_path, storage=CustomStorage())

file = models.FileField(upload_to=some_path)