Static Files Overview
In this page:
What Counts as a Static File
Static files are assets that don't change per request: CSS stylesheets, JavaScript files, images, and fonts. Django's staticfiles app locates and serves them.
Note: Keep static files inside an app's static/<app_name>/ folder to avoid naming collisions with other apps.
Example: What Counts as a Static File
CSS, JS, images, and fonts all belong under an app's static/ folder — these commands create that folder structure and two static files inside it.
mkdir -p myapp/static/myapp
touch myapp/static/myapp/style.css myapp/static/myapp/script.js
⚠️ Run this command in your terminal.
Enabling the Staticfiles App
django.contrib.staticfiles must be listed in INSTALLED_APPS — it ships enabled by default in a new project and handles discovering and serving static files.
Example: Enabling the Staticfiles App
django.contrib.staticfiles must be listed in INSTALLED_APPS — it ships enabled by default in a new project and handles discovering and serving static files.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.staticfiles',
'myapp',
]
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
How Django Finds Static Files
Django searches each installed app's static/ subdirectory automatically, plus any extra directories listed in STATICFILES_DIRS, when resolving a {% static %} tag or serving files in development.
Warning: Two apps with a file at the same relative path can silently shadow each other — namespace by app name.
Example: How Django Finds Static Files
Django searches each installed app's static/ subdirectory automatically, plus any extra directories listed in STATICFILES_DIRS, when resolving a {% static %} tag or serving files in development.
STATICFILES_DIRS = [
BASE_DIR / 'static',
]
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
- Assuming STATICFILES_DIRS is required even for simple projects — Django can serve an app's own static/ folder without it.
- Forgetting to run collectstatic before deploying, so production serves 404s for CSS and JS.
- Hardcoding static file paths like /static/style.css instead of using the {% static %} tag, breaking when STATIC_URL changes.
- Static files are CSS, JavaScript, images, and fonts that Django serves unchanged to the browser.
- Each app can keep its own static/ subdirectory, and Django's staticfiles app collects them all together.
- STATIC_URL defines the URL prefix browsers use to request these files.
- In production, collectstatic gathers every app's static files into one directory for the web server to serve.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: