Configure STATIC_URL and STATICFILES_DIRS in Django
📖 Scenario: You are building a Django website that needs to serve static files like images, CSS, and JavaScript. To do this, you must tell Django where to find these static files and how to refer to them in your templates.
🎯 Goal: Set up Django settings to define the URL path for static files and specify the folder where your static files are stored.
📋 What You'll Learn
Create a variable STATIC_URL with the value '/static/' in settings.py.
Create a list variable STATICFILES_DIRS that includes the path BASE_DIR / 'static'.
Use the BASE_DIR variable which is already defined as the project base directory.
Follow Django's recommended syntax for static files configuration.
💡 Why This Matters
🌍 Real World
Web developers use STATIC_URL and STATICFILES_DIRS to manage and serve static assets like stylesheets and images in Django projects.
💼 Career
Knowing how to configure static files is a basic but crucial skill for Django developers working on real websites.
Progress0 / 4 steps
1
Create BASE_DIR variable
In settings.py, create a variable called BASE_DIR that uses Path(__file__).resolve().parent.parent to get the project base directory.
Django
Hint
Use from pathlib import Path at the top, then set BASE_DIR to the parent of the parent folder of the current file.
2
Set STATIC_URL variable
Add a variable called STATIC_URL in settings.py and set it to the string '/static/'.
Django
Hint
STATIC_URL is the URL prefix for static files. Use the exact string '/static/'.
3
Define STATICFILES_DIRS list
Create a list variable called STATICFILES_DIRS that contains one path: BASE_DIR / 'static'.
Django
Hint
STATICFILES_DIRS is a list of folders where Django looks for static files. Use square brackets and include BASE_DIR / 'static'.
4
Complete settings.py static files setup
Ensure the settings.py file includes the import of Path, the BASE_DIR variable, the STATIC_URL string, and the STATICFILES_DIRS list exactly as shown.
Django
Hint
Check that all lines are present and exactly match the required code.
Practice
(1/5)
1. What is the purpose of STATIC_URL in a Django project?
easy
A. It sets the database connection string for static files.
B. It defines the URL prefix to access static files in the browser.
C. It lists the directories where static files are stored on disk.
D. It configures the caching behavior for static files.
Solution
Step 1: Understand STATIC_URL role
STATIC_URL is used to tell Django what URL prefix to use when serving static files like images or CSS.
Step 2: Differentiate from STATICFILES_DIRS
STATICFILES_DIRS lists 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 B
Quick Check:
STATIC_URL = URL prefix [OK]
Hint: STATIC_URL is the web address prefix for static files [OK]
Common Mistakes:
Confusing STATIC_URL with STATICFILES_DIRS
Thinking STATIC_URL sets file system paths
Assuming STATIC_URL controls caching
2. Which of the following is the correct way to define STATICFILES_DIRS in Django settings?
easy
A. STATICFILES_DIRS = 'staticfiles'
B. STATICFILES_DIRS = '/static/'
C. STATICFILES_DIRS = [BASE_DIR / 'static']
D. STATICFILES_DIRS = {'static': BASE_DIR}
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 C
Quick Check:
STATICFILES_DIRS is a list of folders [OK]
Hint: STATICFILES_DIRS must be a list of folder paths [OK]
What URL will Django use to serve a file named logo.png located inside the assets folder?
medium
A. /static/logo.png
B. /static/assets/logo.png
C. /assets/assets/logo.png
D. /assets/logo.png
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 D
Quick Check:
URL = STATIC_URL + filename [OK]
Hint: URL = STATIC_URL + filename path [OK]
Common Mistakes:
Adding folder name twice in URL
Using default '/static/' URL instead of custom
Confusing file system path with URL path
4. You set STATICFILES_DIRS = BASE_DIR / 'static' without brackets in Django settings. What error will occur?
medium
A. TypeError because STATICFILES_DIRS must be a list or tuple
B. No error, it works fine
C. SyntaxError due to missing quotes
D. FileNotFoundError at runtime
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 A
Quick Check:
STATICFILES_DIRS needs list/tuple [OK]
Hint: Always wrap STATICFILES_DIRS paths in a list or tuple [OK]
Common Mistakes:
Forgetting brackets around STATICFILES_DIRS paths
Assuming single path is accepted
Confusing syntax error with runtime error
5. You want to serve static files from two folders: 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/?