djangodjango-staticfilesdjango-settingsdjango-appsdjango-static

Is there a better way to load Django static directories for multiple apps than this? Thank you


I currently have the following in settings.py:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

#add app-specific static directory
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'project/static'),
    os.path.join(BASE_DIR, 'project/apps/blog/static/'),
    os.path.join(BASE_DIR, 'project/apps/users/static/'),
    os.path.join(BASE_DIR, 'project/apps/comments/static/'),
    os.path.join(BASE_DIR, 'project/apps/categories/static/'),
)

Should I be doing this in one line? Thanks very much.


Solution

  • You can add custom static file finder that would sort you out, but generally if you have /static folder inside of the app it should be discovered by

    django.contrib.staticfiles.finders.AppDirectoriesFinder
    

    as documented

    The default will find files stored in the STATICFILES_DIRS setting (using django.contrib.staticfiles.finders.FileSystemFinder) and in a static subdirectory of each app (using django.contrib.staticfiles.finders.AppDirectoriesFinder). If multiple files with the same name are present, the first file that is found will be used

    Source