Full answer
Both are files, but they have different owners and life cycles.
Static files belong to the code. You put them in each app's static/ folder or in STATICFILES_DIRS. Settings involved:
STATIC_URL = "static/": the URL prefix.STATIC_ROOT: the folder wherepython manage.py collectstaticcopies every static file from every app.- In templates:
{% load static %}then{% static 'css/site.css' %}.
With DEBUG=True, runserver serves static files for you. In production Django does not. Use the web server (Nginx), WhiteNoise (a middleware that serves files from Python with caching headers) or a CDN. ManifestStaticFilesStorage (set through the STORAGES setting in Django 4.2+) adds a content hash to file names like site.3f2a1b.css, so browsers can cache them for a long time and still get new versions after a deploy.
Media files are user uploads, handled by FileField and ImageField.
MEDIA_URLandMEDIA_ROOTset the URL and folder for local storage.- In production, store them in object storage (Amazon S3, Google Cloud Storage, Azure Blob) through
django-storages, because app servers come and go and local disks are not shared. - Treat uploads as untrusted: validate size and type, never serve user-uploaded HTML from your main domain, and use signed or private URLs for private files such as fee receipts.
The model field stores only the file path in the database, not the file bytes.
import sys
import types
import django
from django.conf import settings
# Register a tiny in-memory "school" app so this example runs as one file
sys.modules["school"] = types.ModuleType("school")
sys.modules["school"].__file__ = __file__
settings.configure(
SECRET_KEY="demo-only-key-not-for-production",
ALLOWED_HOSTS=["testserver"], # the host name used by Django's test client
INSTALLED_APPS=["django.contrib.contenttypes", "django.contrib.auth", "school"],
DATABASES={"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": ":memory:"}},
)
django.setup()
import tempfile
from pathlib import Path
from django.core.files.base import ContentFile
from django.core.files.storage import FileSystemStorage
from django.template import Engine, Context
media_root = Path(tempfile.mkdtemp())
settings.STATIC_URL = "/static/"
settings.MEDIA_URL = "/media/"
# Media: a user upload saved through a storage backend
storage = FileSystemStorage(location=media_root, base_url=settings.MEDIA_URL)
name = storage.save("receipts/fee-2026.txt", ContentFile(b"Fee paid: 1200"))
print(name, storage.url(name), storage.size(name))
# Static: a URL built by the {% static %} tag
engine = Engine(libraries={"static": "django.templatetags.static"})
html = engine.from_string("{% load static %}<link href=\"{% static 'css/site.css' %}\">").render(Context())
print(html)
Standalone script. The first lines register a tiny in-memory app called school and an in-memory SQLite database so the file runs with `python file.py`. In a real project the settings live in settings.py and models live in an app's models.py. Output (ran twice with identical results; Python 3.13.5, Django 5.2.20, djangorestframework 3.18.3): receipts/fee-2026.txt /media/receipts/fee-2026.txt 14 <link href="/static/css/site.css">
Key points
- Static = shipped with code; media = uploaded by users
- collectstatic copies static files into STATIC_ROOT
- Django does not serve static files in production by itself
- Hashed file names allow long browser caching
- Store media in object storage and treat uploads as untrusted
Common mistakes
- Serving static files through Django in production with DEBUG=True left on
- Saving uploads on the local disk of a server that is replaced on each deploy
- Trusting the uploaded file name or extension
- Forgetting to run collectstatic in the deployment pipeline
Follow-ups
- Why do hashed static file names help caching?
- How would you serve private media files such as ID cards?
- What does WhiteNoise do and when is it enough?
Tips
- Define each type with an example
- Name one production serving option for each
- Mention upload validation as a security point