How to ignore static files of a specific application using collectstatic only?

Just to get this out of the way, if at all possible, I would like to do this without putting them in a directory with the application name inside the application’s static folder, it feels redundant. If this is the only way, then this is life.

I use:

STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', ) 

and

 STATICFILES_STORAGE = 'pipeline.storage.PipelineCachedStorage' 

which compiles JS and SASS when running collectstatic. They are both located in the APP_ROOT / static / directory.

The only problem is that it combines all the sass and js source files with it. At some point, I am going to push it all towards S3, and I would like to avoid this if possible.

I found that if you run:

 python manage.py collectstatic -i sass -i js 

It still compiles JS and CSS files that I specified, leaving the rest of the source files. Unfortunately, it also ignores every js file in / admin / since it matches / admin / js / etc. I don’t even know if this might be a problem for this particular project, but I can foresee other applications in the future. I definitely want to enable static js / css stored in the application.

What I would like to do is something like:

 python manage.py collectstatic -i app_name/sass -i app_name/js 

And as I mentioned above, a simple solution is to simply prefix my static files in a folder named app_ /, like django.contrib.admin. However, at this point you get the directory structure PROJECT_ROOT / app_name / static / app_name / [js | sass | img | data] /, and I think it should be possible to avoid redundancy.

Again, maybe this is the best option to avoid conflict with other applications?

I have studied the possibility of writing custom repositories and search engines, and I think it is possible to do this on my own. I wanted to check here, although first, to make sure that this is a problem that someone else has solved, or to make sure that the overwhelming answer is simply to add a prefix directory.

If I were to roll on my own, the path that I think I would take would expand django.contrib.staticfiles.finders.AppDirectoriesFinder and override list() . I’m not sure yet that this approach will work, I need to track more how the actions of the collective management team are progressing, so if someone has done this or something like that before or knows why it will / will not work, any help will be appreciated.

Thanks!

+4
source share
2 answers

I managed to solve this problem by subclassing Django finders as follows:

PYTHON 2.X

 from django.contrib.staticfiles import finders from django.conf import settings def add_ignores(ignore_patterns): ignore = settings.STATICFILES_FINDERS_IGNORE if ignore: if ignore_patterns: ignore_patterns.extend(ignore) else: ignore_patterns = ignore return ignore_patterns class FileSystemFinderIgnore(finders.FileSystemFinder): def list(self, ignore_patterns): return super(FileSystemFinderIgnore, self).list(add_ignores(ignore_patterns)) class AppDirectoriesFinderIgnore(finders.AppDirectoriesFinder): def list(self, ignore_patterns): return super(AppDirectoriesFinderIgnore, self).list(add_ignores(ignore_patterns)) class DefaultStorageFinderIgnore(finders.DefaultStorageFinder): def list(self, ignore_patterns): return super(DefaultStorageFinderIgnore, self).list(add_ignores(ignore_patterns)) 

PYTHON 3.X

 from django.contrib.staticfiles import finders from django.conf import settings def add_ignores(ignore_patterns): ignore = settings.STATICFILES_FINDERS_IGNORE if ignore: if ignore_patterns: ignore_patterns.extend(ignore) else: ignore_patterns = ignore return ignore_patterns class FileSystemFinderIgnore(finders.FileSystemFinder): def list(self, ignore_patterns): return super().list(add_ignores(ignore_patterns)) class AppDirectoriesFinderIgnore(finders.AppDirectoriesFinder): def list(self, ignore_patterns): return super().list(add_ignores(ignore_patterns)) class DefaultStorageFinderIgnore(finders.DefaultStorageFinder): def list(self, ignore_patterns): return super().list(add_ignores(ignore_patterns)) 

and adding this to my settings:

 STATICFILES_FINDERS_IGNORE = [ '*.scss', '*.js', ] 
+5
source

I am new to django-pipeline , but I believe that it now has pipeline.finders.FileSystemFinder and pipeline.finders.AppDirectoriesFinder to do just that.

See the section “If You Want to Exclude Pipelinable Content from Your Collected Static Files” at https://django-pipeline.readthedocs.org/en/latest/storages.html .

Also, from source code 1.5:

 class AppDirectoriesFinder(PatternFilterMixin, DjangoAppDirectoriesFinder): """ Like AppDirectoriesFinder, but doesn't return any additional ignored patterns. This allows us to concentrate/compress our components without dragging the raw versions in via collectstatic. """ 

Please note that at the time of writing, this results in empty compressed / reduced content when DEBUG==True , see https://github.com/cyberdelia/django-pipeline/issues/418 . I assume this will be fixed in future versions of the django pipeline.

0
source

All Articles