Question

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

Posted December 22, 2020 525 views
DjangoDigitalOcean App Platform

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"),)

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.

×
4 answers

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

by Jeremy Morris
DigitalOcean Spaces is an object storage solution, ideal for unstructured data such as audio, video, images or large amounts of text. To learn more about Spaces and object storage, you can read through [An Introduction to DigitalOcean...

@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.

@bobbyiliev @kamaln7 We are running the same settings with the DO App platform and spaces that we discussed above but the issue is that every time we change the CSS files in the already existing file the changes don’t reflect.
Only one thing is different that I have added CDN to my spaces.

For example, we have an Admin.css file. If we change anything in it like change the background color etc. Since collectstatic is disabled. We have to run collectstatic manually and after running the collectstatic the modified file doesn’t change on the DO spaces. As a result, we are unable to see any CSS update.

I am not sure why it is happening and after exhausting StackOverflow and trying out different pip module to update static files. We are kind of out of ideas.

Any help on this issue will be highly appreciated. Our development is stalled due to this issue. Kindly let me know if you need any more information.

Submit an Answer