Question

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

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

Submit an answer

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!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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

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