Do we need to add files (in the folder) that White Noise creates to the gitignore list?

611 views Asked by At

I have a django project and using whitenoise for static files serving. When we run py manage.py collectstatic whitenoise, that create lot of files in selected folder in STATIC_ROOT object of settings.py file. It takes up a lot of volume. Do I have to add the folder name I specified in settings.py(STATIC_ROOT) to the gitignore list?

Is this true and safe? And it does not make a problem?

1

There are 1 answers

2
GwynBleidD On BEST ANSWER

./manage.py collectstatic is not coming from the Whitenoise package, but from Django itself.

As for keeping this directory in your git repository, usually the answer is: you shouldn't keep it. But for some projects it may be useful to do it. I'll leave it to you to assess which option is better for you. If you're not sure, just keep it away from your git repository.

To give you the full context, let me quickly explain what collectstatic does.

Django has a built-in system of static files. As Django project can include multiple applications (everything you've listed in INSTALLED_APPS) and each app can require some static files to work properly, Django allows each application to keep their static files in their directories. This approach by itself is not perfect when you want to host your application, as all locations where static files are saved inside applications will have to be explicitly listed in your web server configuration.

That's why the collectstatic command exists. After executing it, Django will copy all static files from all your applications to a single location, from which you can serve them at once. You can also extend this list of files by using STATICFILES_DIRS to include some locations outside of your installed apps (for example from your project root).

This approach will of course require running collectstatic command when deploying or updating already deployed project somewhere. Sometimes you can't add such commands to your deployment process, so you have to have already collected static files. One of the approaches is to keep them in your git repository, but this should be avoided and other solutions should be preferred instead (like keeping all your static files on a separate location, for example S3 bucket).