1

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?

Ali Nazari
  • 43
  • 1
  • 6
  • Hi. Welcome to StackOverflow. Yes. You should not be pushing the STATIC_ROOT directory to github. What you should do instead, is run `python manage.py collectstatic` on your server during deployment. – NduJay May 11 '21 at 19:35

1 Answers1

0

./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).

GwynBleidD
  • 20,081
  • 5
  • 46
  • 77
  • I know collectstatic what doing ( and it is from django ). BUT when using white noise STATICFILES_STORAGE this is doing some additional. I know what Django static file system does. So it does not matter if I add or not, there is no problem? So I add because it increases the volume of the tank. And this is bad. – Ali Nazari May 11 '21 at 15:40
  • You should avoid adding any files to your repository that are produced/computed entirely from other files in your repository. This includes collected static files. Whitenoise storages are adding manifest files and compression, which is fully reproducible based on the rest of your project, so it does not change that approach. – GwynBleidD May 11 '21 at 18:32