Bird
Raised Fist0
Djangoframework~20 mins

STATIC_URL and STATICFILES_DIRS in Django - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Static Files Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Purpose of STATIC_URL in Django
What is the main purpose of the STATIC_URL setting in a Django project?
AIt defines the URL prefix used to serve static files in templates and browsers.
BIt lists all directories where Django looks for static files during development.
CIt specifies the absolute file system path where static files are collected.
DIt configures the database table used to store static file metadata.
Attempts:
2 left
💡 Hint
Think about how static files are referenced in HTML pages.
🧠 Conceptual
intermediate
1:30remaining
Role of STATICFILES_DIRS in Django
What does the STATICFILES_DIRS setting do in a Django project?
AIt defines the directory where static files are collected for production deployment.
BIt sets the URL path prefix for static files served by the web server.
CIt tells Django where to find additional static files during development besides app static folders.
DIt configures caching behavior for static files in the browser.
Attempts:
2 left
💡 Hint
Think about where Django looks for static files when running the development server.
❓ component_behavior
advanced
2:00remaining
Effect of STATIC_URL on template static file links
Given STATIC_URL = '/assets/' in settings, what will be the rendered HTML output of {% static 'css/style.css' %} in a Django template?
A<link href="/media/css/style.css" rel="stylesheet">
B<link href="/static/css/style.css" rel="stylesheet">
C<link href="css/style.css" rel="stylesheet">
D<link href="/assets/css/style.css" rel="stylesheet">
Attempts:
2 left
💡 Hint
The {% static %} tag uses STATIC_URL as prefix.
📝 Syntax
advanced
1:30remaining
Correct STATICFILES_DIRS syntax
Which of the following is the correct way to set STATICFILES_DIRS in Django settings to include a folder named 'assets' inside the project root?
ASTATICFILES_DIRS = {'assets'}
BSTATICFILES_DIRS = [BASE_DIR / 'assets']
CSTATICFILES_DIRS = BASE_DIR / 'assets'
DSTATICFILES_DIRS = ('assets')
Attempts:
2 left
💡 Hint
STATICFILES_DIRS expects a list or tuple of paths.
🔧 Debug
expert
2:30remaining
Why static files not loading in production?
In production, static files are not loading even though STATIC_URL = '/static/' and STATICFILES_DIRS is set correctly. What is the most likely cause?
AThe <code>collectstatic</code> command was not run to gather static files into <code>STATIC_ROOT</code>.
BThe <code>STATIC_URL</code> is missing a trailing slash.
CThe <code>STATICFILES_DIRS</code> list contains relative paths instead of absolute paths.
DThe Django development server is running instead of a production server.
Attempts:
2 left
💡 Hint
In production, static files must be collected to one folder.

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

  1. 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.
  2. Step 2: Differentiate from STATICFILES_DIRS

    STATICFILES_DIRS lists folders on disk, not URLs. The other options are unrelated to URL prefix.
  3. Final Answer:

    It defines the URL prefix to access static files in the browser. -> Option B
  4. 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

  1. Step 1: Recognize STATICFILES_DIRS type

    It must be a list or tuple of folder paths, not a string or dict.
  2. 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.
  3. Final Answer:

    STATICFILES_DIRS = [BASE_DIR / 'static'] -> Option C
  4. Quick Check:

    STATICFILES_DIRS is a list of folders [OK]
Hint: STATICFILES_DIRS must be a list of folder paths [OK]
Common Mistakes:
  • Using a string instead of a list
  • Using a dictionary instead of a list
  • Forgetting to use BASE_DIR or full path
3. Given the settings:
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?
medium
A. /static/logo.png
B. /static/assets/logo.png
C. /assets/assets/logo.png
D. /assets/logo.png

Solution

  1. Step 1: Understand STATIC_URL usage

    STATIC_URL sets the URL prefix for static files. Here it is '/assets/'.
  2. 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.
  3. Final Answer:

    /assets/logo.png -> Option D
  4. 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

  1. Step 1: Check STATICFILES_DIRS expected type

    It must be a list or tuple of paths, not a single path object.
  2. Step 2: Analyze given assignment

    Assigning a single path without brackets makes it a path object, not a list, causing a TypeError.
  3. Final Answer:

    TypeError because STATICFILES_DIRS must be a list or tuple -> Option A
  4. 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/?
hard
A. STATIC_URL = '/static/' STATICFILES_DIRS = [BASE_DIR / 'assets', BASE_DIR / 'common_static']
B. STATIC_URL = '/assets/' STATICFILES_DIRS = [BASE_DIR / 'static', BASE_DIR / 'common_static']
C. STATIC_URL = '/static/' STATICFILES_DIRS = BASE_DIR / 'assets' + BASE_DIR / 'common_static'
D. STATIC_URL = '/common_static/' STATICFILES_DIRS = [BASE_DIR / 'assets']

Solution

  1. Step 1: Set STATIC_URL to desired URL prefix

    To serve all static files under '/static/', set STATIC_URL = '/static/'.
  2. 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.
  3. Step 3: Avoid incorrect concatenation or mismatched URLs

    Concatenating paths or mismatching URL prefixes causes errors or missing files.
  4. Final Answer:

    STATIC_URL = '/static/' STATICFILES_DIRS = [BASE_DIR / 'assets', BASE_DIR / 'common_static'] -> Option A
  5. Quick Check:

    Multiple folders in list, one URL prefix [OK]
Hint: Use list for multiple folders, one STATIC_URL prefix [OK]
Common Mistakes:
  • Concatenating paths instead of listing them
  • Using different URL prefixes for folders
  • Forgetting to include all folders in list
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard