Adding Media Files To Heroku

I am trying to deploy a site on heroku that has media files (images I uploaded to a database) and am confused how to display them in production mode. I have my images stored in my media directory.

settings.py

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

urls.py

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

models.py

class Project(models.Model):
    title = models.CharField(max_length=100)
    description = models.CharField(max_length=250)
    tools = models.CharField(max_length=200, default="Some String")
    #image is stored in media/portfolio/images
    image = models.ImageField(upload_to='portfolio/images')
    url = models.URLField(blank=True)

    def _str_(self):
        return self.title

Are the actual images stored in the database or in the folder represented by settings.MEDIA_ROOT with the paths being stored in the database? The latter is what Django provides out of the box.

If you’re storing them on your filesystem with the paths being stored in the db, then take a look at django storages. You’ll need to upload your media files to another hosting provider such as S3, digital ocean, Azure, Google Cloud Platform, etc.

1 Like

They are being stored on the file system with the db path. I will look look into what you wrote.

1 Like

heroku doesn’t actually store media files, I think they get deleted after something like 24 hours on the free version! Found this out the hard way