Jinho  Park
Jinho Park

Reputation: 11

Unexpected directories and files while using collectstatic in django

This is my django project, there is two apps; polls and study

This is the setting of my static file settings.

STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR,"study","static","HScard"),
)
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")

I expected the 'python manage.py collectstatic' in the shell would copy static files to "staticfiles" from only in "study/static/HSCard"(due to STATICFILES_DIRS above).

However, "collectstatic" copied unexpected files below.

enter image description here

Why are the files in admin and polls copied to staticfiles?? besides the files in admin folder were from here (a single example):

Copying 'C:\ProgramData\Anaconda3\lib\site-packages\django\contrib\admin\static\admin\js\vendor\xregexp\xregexp.min.js'

Why does 'collectstatic' working like this and how can I fix it as I expected?

The version of django using is 2.0.7. Thank you.

Upvotes: 0

Views: 240

Answers (1)

gatlanticus
gatlanticus

Reputation: 1226

The problem is due to the default Staticfiles Finders settings:

STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

The AppDirectoriesFinder will look through your apps listed in your INSTALLED_APPS setting. If there are files such as admin/static/, which are included in the admin by default, then they will also be found by collect_static. Your choices are to either:

  1. Remove apps from INSTALLED_APPS (obviously they won't work any more, but they won't contribute static files either)
  2. Add the STATICFILES_FINDERS setting as above, but without the AppDirectoriesFinder setting. Then Django will only collect staticfiles that you have explicitly mentioned in STATICFILES_DIRS

Upvotes: 0

Related Questions