Report this

What is the reason for this report?

Media files are deleting after every deployment to DO Apps + Django

Posted on December 22, 2020

We have a Django project that we are testing with DO Apps for deployments and possibly use in production.

We have some user-generated files like user profile pictures etc. Every time I deploy some new code or some changes. The pipeline automatically deletes the media files. Here is the code that I am using in my Django project settings file.

The code below creates mediafiles and staticfiles folder in my project directory. On the front end, it uploads and serves the file correctly but user profile pictures (media files) are gone after every deployment.

Any urgent help from the community and DO support will be highly appreciated. This is a big blocker for us for going into production.

P.S: Static files are working as expected.

STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)

MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "mediafiles")
MEDIAFILES_DIRS = (os.path.join(BASE_DIR, "media"),)


This 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!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there @kashifShrimp,

As a new container is generated on each deployment, you would need to implement persistent storage like Spaces.

Then configure your application so that it uploads the media assets to the S3 storage rather than locally on the container itself.

You can follow the steps here on how to implement Spaces with Django:

https://www.digitalocean.com/community/tutorials/how-to-set-up-object-storage-with-django

Regards, Bobby

@bobbyiliev I created the spaces with a project but now the problem is that deployment is failing due to collect static command being automated. Is there a way to login to Heroku via App console to disable the collect static at Heroku or any other way?

Here is the exact error message.

 directories, files = storage.listdir(location)
File "/app/.heroku/python/lib/python3.8/site-packages/django/core/files/storage.py", line 316, in listdir
 for entry in os.scandir(path):
 FileNotFoundError: [Errno 2] No such file or directory: '/workspace/crmbase/crmbase/static'

 !     Error while running '$ python manage.py collectstatic --noinput'.
 See traceback above for details.

 You may need to update application code to resolve this error.
 Or, you can disable collectstatic for this application:

$ heroku config:set DISABLE_COLLECTSTATIC=1

 https://devcenter.heroku.com/articles/django-assets
crm-base-dev | 02:17:16 ERROR: failed to build: exit status 1

@bobbyiliev @kamaln7 Thank you for your help.

I have added the above-mentioned settings media files are now working as expected. However, some static files are working and some are not working like css icons.

P.S: I did run collectstatic on the digital ocean app console.

Updated: Never mind the issue can be solved with adding COR settings on DO Spaces.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.