I’ve got a question about serving media files and static files from an S3 bucket. I’d be extremely grateful if anyone could give some pointers about how to solve the problem.
• I’ve completed a number of tutorials to get a basic idea for how Django works. To cement what I’ve learnt, I’ve decided to repurpose a lot of the concepts into a simple web app and deploy it online.
• The site shows a list of conferences, which have a one-to-many relationship with a conference series (e.g. DjangoCon parent with DjangoCon 2019 and DjangoCon 2020 as children).
• The conference series has a logo image as part of its model. The image is displayed as part of a card in a list of all conferences.
• These are all added through the admin, as it’s not intended for users to be able to update the conference list.
• Everything worked fine when working locally, BUT when I deployed to Heroku the logos would not display (even though the files were visible in the admin view in the deployed app).
• A quick search revealed that Heroku cannot serve media files, so I chose the (seemingly) most common approach – adding boto3 and django-storages to my project, and pointing it at an Amazon S3 bucket.
• I followed a number of popular tutorials to set this up.
• However it looks like the app is still looking at “itself” for the media and static files, not the S3 bucket. I can tell this because:
- Simply, the media files and static files are not in the S3 bucket, even though I’ve followed the tutorials and added the AWS credentials to Django.
- When I open the developer view in Chrome, the source for the images + CSS files looks like the local path (there’s no reference to “aws” or “s3” in each file’s path).
• After doing some more digging + basic troubleshooting, I think it could be a couple of things:
- Whitenoise is still set up in my project (from when I was working locally with only static files). Should this be removed? Is it conflicting with boto3 and django-storages?
- Do I need to update my models in some way (e.g. with the
- Do I need to migrate the database (or any other database task) to update the URLs for each model’s image file?
- Is the fact that the app is running in a Docker container causing an issue with communicating “out” to the S3 bucket?
Although to be honest, I’m completely lost! I can’t find any resources online that explain this issue and how to resolve it.
I’d be extremely grateful for any advice.
I haven’t seen anyone post long tracts of code in this forum, so if you need to see the settings.py, storage_backends.py and conferences/models.py files, they’re in this stack overflow ticket.