How do you handle static and media files in Django?
Learn how to handle static and media files in Django: STATIC_ROOT, collectstatic, MEDIA_ROOT, serving uploads, WhiteNoise and S3, with settings examples.
Expected Interview Answer
In Django, static files are the developer-provided assets your site ships with — CSS, JavaScript, and images — while media files are user-uploaded content stored at runtime. They are configured and served separately: static files through STATIC_URL/STATIC_ROOT and collectstatic, media files through MEDIA_URL/MEDIA_ROOT and model FileField/ImageField uploads.
For static files you set STATIC_URL, list source folders in STATICFILES_DIRS during development, and run collectstatic to gather everything into STATIC_ROOT for production, where a web server, WhiteNoise, or a CDN serves them. For media files you set MEDIA_URL and MEDIA_ROOT; uploads arrive through forms into FileField or ImageField and are written under MEDIA_ROOT. In development you can serve media via static() in urls.py, but in production media should be served by the web server or object storage such as S3 — never by Django itself. The key distinction is that static assets are known and versioned at deploy time, whereas media is unpredictable, user-generated, and must be kept out of source control.
- Clear separation of shipped assets from user uploads
- collectstatic bundles assets for efficient production serving
- CDN or WhiteNoise offloads static delivery from Django
- MEDIA_ROOT isolates untrusted user content
- Object storage (S3) scales media independently of the app
AI Mentor Explanation
Static files are the fixed ground fittings — the sightscreens, boundary boards, and printed scorecards prepared before play — while media files are the photos and clips fans upload during the match. The stadium ships the fittings with the venue; the fan content arrives unpredictably and is stored in a separate gallery.
Step-by-Step Explanation
Step 1
Configure static settings
Set STATIC_URL, add source folders to STATICFILES_DIRS, and set STATIC_ROOT for the collected output.
Step 2
Collect for production
Run python manage.py collectstatic to gather all app and project static files into STATIC_ROOT.
Step 3
Serve static assets
Let a web server, WhiteNoise, or a CDN serve the collected files — Django should not serve them in production.
Step 4
Configure media settings
Set MEDIA_URL and MEDIA_ROOT so uploads via FileField/ImageField are written to a known directory.
Step 5
Serve media safely
In development use static() in urls.py; in production serve media through the web server or object storage like S3.
What Interviewer Expects
- Static = shipped assets, media = user uploads
- Role of STATIC_URL, STATIC_ROOT, and collectstatic
- Role of MEDIA_URL and MEDIA_ROOT with FileField/ImageField
- Django should not serve static or media in production
- Awareness of CDN/WhiteNoise and S3-style object storage
Common Mistakes
- Confusing STATICFILES_DIRS (sources) with STATIC_ROOT (collected output)
- Serving media through Django in production
- Forgetting to run collectstatic before deployment
- Committing user media into version control
- Not validating or sandboxing user-uploaded files
Best Answer (HR Friendly)
“Static files are the fixed assets a site is built with, like its CSS, JavaScript, and logos, while media files are things users upload, like photos or documents. Django keeps them in separate places and collects static files for production, while user uploads are stored on their own and usually served by a web server or cloud storage rather than by Django.”
Code Example
# settings.py
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'staticfiles' # collectstatic target
STATICFILES_DIRS = [BASE_DIR / 'assets'] # source folders
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media' # user uploads land here
# urls.py (development only)
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... your routes ...
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)Follow-up Questions
- What does collectstatic actually do and when do you run it?
- Why should Django not serve static or media files in production?
- How would you store user uploads on Amazon S3?
- What is the difference between STATICFILES_DIRS and STATIC_ROOT?
- How does WhiteNoise help serve static files?
MCQ Practice
1. Which setting defines where collectstatic gathers files for production?
STATIC_ROOT is the single directory collectstatic copies all static assets into for production serving.
2. Where are user-uploaded files stored in Django?
Uploads via FileField/ImageField are written under MEDIA_ROOT, referenced through MEDIA_URL.
3. How should media files be served in production?
Django should not serve media in production; a web server or object storage such as S3 handles it efficiently and safely.
Flash Cards
Static vs media files? — Static are developer-shipped assets (CSS/JS/images); media are user-uploaded files at runtime.
What does collectstatic do? — Gathers all static files from apps and STATICFILES_DIRS into STATIC_ROOT for production.
Where do uploads go? — Into MEDIA_ROOT, served under MEDIA_URL, via FileField or ImageField.
Who serves files in production? — A web server, WhiteNoise, or a CDN/object storage — not Django itself.