I’m having issues with Django’s static files not being uploaded to Digital Ocean Spaces (S3-compatible storage) in production since I’ve upgraded to Django 5.2 from 4.2. As part of this upgrade I also updated python, boto3 and django-storages.
The collectstatic
command appears to run successfully locally, but files are not being updated in the Spaces bucket. Additionally, some static files (particularly CSS) are being served as downloads instead of being rendered in the browser.
django-storages
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
django = "==5.2"
pillow = "*"
python-dotenv = "*"
stripe = "*"
mailchimp-marketing = "*"
dotenv = "*"
gunicorn = "*"
psycopg2 = "*"
django-storages = "==1.14.2"
boto3 = "==1.34.69"
weasyprint = "*"
[dev-packages]
[requires]
python_version = "3.12"
# settings.py (production settings)
AWS_ACCESS_KEY_ID = os.getenv('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.getenv('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = 'equine-pro'
AWS_S3_ENDPOINT_URL = 'https://ams3.digitaloceanspaces.com'
AWS_S3_OBJECT_PARAMETERS = {
'CacheControl': 'max-age=86400',
'ACL': 'public-read'
}
AWS_DEFAULT_ACL = 'public-read'
AWS_LOCATION = 'static'
AWS_S3_CUSTOM_DOMAIN = 'equine-pro.ams3.cdn.digitaloceanspaces.com'
AWS_S3_REGION_NAME = 'ams3'
AWS_S3_FILE_OVERWRITE = True
AWS_QUERYSTRING_AUTH = False
AWS_S3_VERIFY = True
AWS_S3_SIGNATURE_VERSION = 's3v4'
AWS_S3_ADDRESSING_STYLE = 'virtual'
STATICFILES_STORAGE = 'equineproclinic.storage_backends.StaticStorage'
# storage_backends.py
from storages.backends.s3boto3 import S3Boto3Storage
import logging
logger = logging.getLogger(__name__)
class StaticStorage(S3Boto3Storage):
location = 'static'
default_acl = 'public-read'
def _save(self, name, content):
logger.info(f"Attempting to save {name}")
try:
# Set content type based on file extension
content_type = self._get_content_type(name)
params = {
'ACL': self.default_acl,
'CacheControl': 'max-age=86400',
'ContentType': content_type
}
return super()._save(name, content, extra_args=params)
except Exception as e:
logger.error(f"Error saving {name}: {str(e)}")
raise
Any help or guidance would be greatly appreciated!
collectstatic
command runs without errorsThis textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Heya, @jorienwalraven
You can check if collectstatic
is using your custom storage
python manage.py shell
>>> from django.conf import settings
>>> >>> settings.STATICFILES_STORAGE
Make sure it returns equineproclinic.storage_backends.StaticStorage
. If not, Django isn’t picking up the right backend.
Also, sometimes the hashed filenames make Django think nothing changed:
python manage.py collectstatic --noinput --clear --ignore=*.gz
Add --verbosity 3
to confirm which files are uploaded.
Hope that this helps!
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.