STATIC_URL and STATICFILES_DIRS help Django find and serve your static files like images, CSS, and JavaScript. They make your website look and work nicely.
STATIC_URL and STATICFILES_DIRS in Django
STATIC_URL = '/static/' STATICFILES_DIRS = [ BASE_DIR / 'static', ]
STATIC_URL is the web address prefix for static files.
STATICFILES_DIRS is a list of folders where Django looks for static files.
STATIC_URL = '/assets/' STATICFILES_DIRS = [ BASE_DIR / 'assets', ]
STATIC_URL = '/static/' STATICFILES_DIRS = [ BASE_DIR / 'static', BASE_DIR / 'extra_static', ]
This example sets up STATIC_URL and STATICFILES_DIRS using BASE_DIR. It prints the URL prefix and the list of folders where Django will look for static files.
from pathlib import Path BASE_DIR = Path(__file__).resolve().parent STATIC_URL = '/static/' STATICFILES_DIRS = [ BASE_DIR / 'static', ] print(f"Static URL is: {STATIC_URL}") print(f"Static files directories: {STATICFILES_DIRS}")
Always end STATIC_URL with a slash '/' to avoid URL problems.
STATICFILES_DIRS is optional if you only use app static folders.
In production, use collectstatic to gather all static files in one place.
STATIC_URL sets the web path prefix for static files.
STATICFILES_DIRS tells Django where to find extra static files outside apps.
These settings help your website load images, styles, and scripts correctly.
Practice
STATIC_URL in a Django project?Solution
Step 1: Understand STATIC_URL role
STATIC_URLis used to tell Django what URL prefix to use when serving static files like images or CSS.Step 2: Differentiate from STATICFILES_DIRS
STATICFILES_DIRSlists folders on disk, not URLs. The other options are unrelated to URL prefix.Final Answer:
It defines the URL prefix to access static files in the browser. -> Option BQuick Check:
STATIC_URL = URL prefix [OK]
- Confusing STATIC_URL with STATICFILES_DIRS
- Thinking STATIC_URL sets file system paths
- Assuming STATIC_URL controls caching
STATICFILES_DIRS in Django settings?Solution
Step 1: Recognize STATICFILES_DIRS type
It must be a list or tuple of folder paths, not a string or dict.Step 2: Check syntax correctness
STATICFILES_DIRS = [BASE_DIR / 'static'] uses a list with a path expression (BASE_DIR / 'static'), which is correct syntax in modern Django.Final Answer:
STATICFILES_DIRS = [BASE_DIR / 'static'] -> Option CQuick Check:
STATICFILES_DIRS is a list of folders [OK]
- Using a string instead of a list
- Using a dictionary instead of a list
- Forgetting to use BASE_DIR or full path
STATIC_URL = '/assets/' STATICFILES_DIRS = [BASE_DIR / 'assets']
What URL will Django use to serve a file named
logo.png located inside the assets folder?Solution
Step 1: Understand STATIC_URL usage
STATIC_URL sets the URL prefix for static files. Here it is '/assets/'.Step 2: Map file location to URL
The file 'logo.png' inside the 'assets' folder will be served at '/assets/logo.png' because STATICFILES_DIRS points to that folder.Final Answer:
/assets/logo.png -> Option DQuick Check:
URL = STATIC_URL + filename [OK]
- Adding folder name twice in URL
- Using default '/static/' URL instead of custom
- Confusing file system path with URL path
STATICFILES_DIRS = BASE_DIR / 'static' without brackets in Django settings. What error will occur?Solution
Step 1: Check STATICFILES_DIRS expected type
It must be a list or tuple of paths, not a single path object.Step 2: Analyze given assignment
Assigning a single path without brackets makes it a path object, not a list, causing a TypeError.Final Answer:
TypeError because STATICFILES_DIRS must be a list or tuple -> Option AQuick Check:
STATICFILES_DIRS needs list/tuple [OK]
- Forgetting brackets around STATICFILES_DIRS paths
- Assuming single path is accepted
- Confusing syntax error with runtime error
assets and common_static inside your project. How should you configure STATICFILES_DIRS and STATIC_URL to serve files correctly under the URL prefix /static/?Solution
Step 1: Set STATIC_URL to desired URL prefix
To serve all static files under '/static/', setSTATIC_URL = '/static/'.Step 2: List all static folders in STATICFILES_DIRS
Include both folders as a list:[BASE_DIR / 'assets', BASE_DIR / 'common_static']so Django finds files in both.Step 3: Avoid incorrect concatenation or mismatched URLs
Concatenating paths or mismatching URL prefixes causes errors or missing files.Final Answer:
STATIC_URL = '/static/' STATICFILES_DIRS = [BASE_DIR / 'assets', BASE_DIR / 'common_static'] -> Option AQuick Check:
Multiple folders in list, one URL prefix [OK]
- Concatenating paths instead of listing them
- Using different URL prefixes for folders
- Forgetting to include all folders in list
Ready to prove it?
Pass the test and this pattern moves unknown
