Django static and media files + DigitalOcean Spaces

Hy everyone!
This is my first time trying to connect a Django project (which is in DigitalOcean App Platform) with DigitalOcean Spaces. I created 3 folders inside Spaces: static, media and staticfiles. I noticed however that when I run 'collectstatic', all static files are copied not to the 'staticfiles' folder but to the root-level.

If I visit the domain name, the static files are visible: both the images and the css and javascript files. Conversely, media files are not displayed.

Inspecting the code via google developer tools I noticed that the src attributes of the img tags try to find both static and media files at the root level.

This is what I tried to do.

I installed 'django-storages', included it in 'INSTALLED_APPS' and then put this code in settings.py:

AWS_ACCESS_KEY_ID = os.getenv('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.getenv('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = os.getenv('AWS_STORAGE_BUCKET_NAME')
# AWS_S3_CUSTOM_DOMAIN = os.getenv('AWS_S3_CUSTOM_DOMAIN')
AWS_S3_REGION_NAME = '<REGION_NAME>'
AWS_S3_ENDPOINT_URL = os.getenv('AWS_S3_ENDPOINT_URL')
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
STATICFILES_DIRS = [BASE_DIR / "static"]
MEDIA_URL = 'https://<REGION_NAME>.digitaloceanspaces.com/<BUCKET_NAME>/media/'
STATIC_URL = 'https://<REGION_NAME>.digitaloceanspaces.com/<BUCKET_NAME>/static/'
MEDIA_ROOT = 's3://<REGION_NAME>.digitaloceanspaces.com/<BUCKET_NAME>/media'
STATIC_ROOT = 's3://<REGION_NAME>.digitaloceanspaces.com/<BUCKET_NAME>/staticfiles'

In 'django-storages' docs I saw that STATICFILES_STORAGE should be like this:

STATICFILES_STORAGE = 'storages.backends.s3boto3.S3StaticStorage'

but this way the static files were not loaded (css, images, js ecc.)

Back to Top