Challenge - 5 Problems
Static Files Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
How does Django serve static files in development?
In a default Django development setup, what happens when you request a static file like CSS or JavaScript from the browser?
Attempts:
2 left
💡 Hint
Think about what DEBUG mode does in Django and how static files are handled by the development server.
✗ Incorrect
When DEBUG is True (default in development), Django's runserver automatically serves static files using the staticfiles app. This means you don't need extra setup to see CSS or JS files.
📝 Syntax
intermediate2:00remaining
Correct STATICFILES_DIRS setting syntax
Which of the following is the correct way to define STATICFILES_DIRS in Django settings to include a folder named 'assets' inside your project directory?
Attempts:
2 left
💡 Hint
Remember STATICFILES_DIRS expects a list or tuple of paths, and BASE_DIR is a Path object.
✗ Incorrect
STATICFILES_DIRS should be a list or tuple of directories. Using BASE_DIR / 'assets' uses pathlib to join paths correctly.
🔧 Debug
advanced2:00remaining
Why are static files not loading in development?
You have DEBUG=True and static files in your app's static folder, but CSS is not loading in the browser. What is the most likely cause?
Attempts:
2 left
💡 Hint
Check if the app responsible for static files is enabled.
✗ Incorrect
The staticfiles app must be in INSTALLED_APPS for Django to serve static files in development. collectstatic is only needed for production.
🧠 Conceptual
advanced2:00remaining
Role of collectstatic in development vs production
What is the purpose of the 'collectstatic' command and when should you use it?
Attempts:
2 left
💡 Hint
Think about how static files are served differently in development and production.
✗ Incorrect
collectstatic copies all static files into a single folder (STATIC_ROOT) for production servers to serve efficiently. In development, Django serves files directly from app folders.
❓ state_output
expert2:00remaining
What is the value of STATIC_URL after this settings code?
Given the following Django settings snippet, what is the value of STATIC_URL?
BASE_DIR = Path(__file__).resolve().parent.parent
STATIC_URL = '/static/'
if not DEBUG:
STATIC_URL = '/assets/'
Django
from pathlib import Path BASE_DIR = Path(__file__).resolve().parent.parent DEBUG = True STATIC_URL = '/static/' if not DEBUG: STATIC_URL = '/assets/'
Attempts:
2 left
💡 Hint
Check the value of DEBUG and how it affects the if condition.
✗ Incorrect
Since DEBUG is True, the condition 'if not DEBUG' is False, so STATIC_URL remains '/static/'.