0
0
Djangoframework~20 mins

Static files in development in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Static Files Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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?
ADjango cannot serve static files in development; you must use a separate web server.
BDjango requires you to manually copy static files to the templates folder to serve them.
CDjango serves static files only if DEBUG is set to False.
DDjango automatically serves static files using the staticfiles app and the development server without extra configuration.
Attempts:
2 left
💡 Hint
Think about what DEBUG mode does in Django and how static files are handled by the development server.
📝 Syntax
intermediate
2: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?
ASTATICFILES_DIRS = ('assets',)
BSTATICFILES_DIRS = [BASE_DIR / 'assets']
CSTATICFILES_DIRS = BASE_DIR + '/assets/'
DSTATICFILES_DIRS = {'assets'}
Attempts:
2 left
💡 Hint
Remember STATICFILES_DIRS expects a list or tuple of paths, and BASE_DIR is a Path object.
🔧 Debug
advanced
2: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?
AYou forgot to add 'django.contrib.staticfiles' to INSTALLED_APPS.
BYou did not run 'python manage.py collectstatic' in development.
CYou set STATIC_URL to an incorrect value like '/media/'.
DYou need to set DEBUG=False to serve static files.
Attempts:
2 left
💡 Hint
Check if the app responsible for static files is enabled.
🧠 Conceptual
advanced
2:00remaining
Role of collectstatic in development vs production
What is the purpose of the 'collectstatic' command and when should you use it?
AIt compiles CSS and JavaScript files automatically during development.
BIt serves static files directly from the app folders during development.
CIt gathers all static files into STATIC_ROOT for production; not needed in development with DEBUG=True.
DIt deletes all static files from the project to free space.
Attempts:
2 left
💡 Hint
Think about how static files are served differently in development and production.
state_output
expert
2: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/'
A/static/
B/assets/
C'' (empty string)
DNone
Attempts:
2 left
💡 Hint
Check the value of DEBUG and how it affects the if condition.