Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use django-storages for media storage on dropbox?

I am using django-storages to serve media files on Dropbox. But I cannot get it working (media files are still stored in local server).

I've installed dropbox and django-storages, and created an app with permission type:app folder, then added related settings.

Here's my settings:

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',
)

To be able to upload the media files to Dropbox (and reading them) what has to be changed in the settings code?

like image 587
S_M Avatar asked Oct 29 '22 15:10

S_M


1 Answers

Ok, found the problem! The settings are correct, the problem was that for some reason I was using a custom storage for my FileField! All I had to do was removing the storage=...:

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

to

file = models.FileField(upload_to=some_path)
like image 78
S_M Avatar answered Nov 14 '22 17:11

S_M