What is STATICFILES_DIRS in Django: Explanation and Example
STATICFILES_DIRS in Django is a setting that tells Django where to look for static files like images, CSS, and JavaScript outside of each app's static folder. It is a list of folder paths that Django will search when collecting static files for your project.How It Works
Imagine you have a big art studio with many rooms (apps), and each room has its own supplies (static files). But sometimes, you want to keep some supplies in a shared closet outside the rooms. STATICFILES_DIRS is like telling Django where that shared closet is.
When Django needs to gather all the static files to serve them, it looks inside each app's static folder and also checks the folders listed in STATICFILES_DIRS. This way, you can organize your static files both inside apps and in common places.
Example
This example shows how to set STATICFILES_DIRS in your Django settings.py to include a folder named assets at the project root.
import os BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'assets'), ]
When to Use
Use STATICFILES_DIRS when you have static files that are not tied to a specific app but shared across your whole project. For example, a common CSS file or JavaScript library used by multiple apps.
This setting is helpful in bigger projects where you want to keep shared static files organized separately from app-specific files.
Key Points
- STATICFILES_DIRS is a list of folders for extra static files outside apps.
- Django searches these folders when collecting static files.
- It helps organize shared static resources in one place.
- Works together with app static folders and
STATIC_ROOT.
Key Takeaways
STATICFILES_DIRS tells Django where to find extra static files outside app folders.STATICFILES_DIRS as a Python list in settings.py.