← Back to Django Course | Chapter 11: Static Files & Media Handling | Lesson 1 of 8

Static Files Overview

Static files are the pictures, style sheets, and scripts a website hands out exactly as they are, without changing them, like handing someone a printed flyer instead of writing a new one each time.

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.

bash
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.

markup
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.

markup
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. #}
Common Mistakes
  1. Assuming STATICFILES_DIRS is required even for simple projects — Django can serve an app's own static/ folder without it.
  2. Forgetting to run collectstatic before deploying, so production serves 404s for CSS and JS.
  3. Hardcoding static file paths like /static/style.css instead of using the {% static %} tag, breaking when STATIC_URL changes.
Chapter Summary
  • 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.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.