0
votes

To serve the static files (CSS, JS), I set the settings:

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]

This means that all my static files need to be in the project root's static folder

/app1
/app2
/media
/static #They need to be stored here, all static files
/templates
urls.py
settings.py
manage.py

However, I am storing them in my app's static folder.

/app1/static/ # Storing the static files here
/app2/static/ #and here
/media/
/static/ # but not here

Still, Django is able to serve them, how is that possible?

I tried the same thing with media files; setting

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

and storing the media (images etc) files in each app's individual media dir. This time, Django did not serve the files and served them only when either I moved the files to the project root's media dir or changed the setting to MEDIA_ROOT = os.path.join(BASE_DIR, '<app_name>/media')

Why was I allowed to serve static files even when they weren't in the project root static dir but not the media files - they were only served from the root media dir.

1
It would be nice to mark this question as answered. Thank you! - raratiru

1 Answers

3
votes

Please read the documentation carefully - https://docs.djangoproject.com/en/1.9/ref/settings/#staticfiles-finders

It precisely states that django will search for static files in each app + the directory stated in settings.

Quoting as in the documentation -

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.

While this is not true for media files. Django doesn't look for media files in subdirectories.