Static & Media Files
Static & Media Files
In Django architecture, Static Files and Media Files serve two completely different purposes:
- Static Files: Assets authored and committed by developers (CSS stylesheets, JavaScript bundles, site logos, SVG icons).
- Media Files: User-uploaded content (user profile avatars, PDF invoices, product catalog images, video attachments).
In ephemeral cloud environments (Heroku, Kubernetes, AWS ECS), container file systems are destroyed upon restart. Storing user media files on the container disk causes data loss. Enterprise systems offload static and media storage to cloud object stores like Amazon S3 using django-storages.
1. Static vs Media Files Overview
| Category | Authored By | Source Control | Storage in Production | Example |
|---|---|---|---|---|
| Static Files | Developers | Committed to Git | WhiteNoise or AWS S3 / CDN | css/styles.css, bundle.js |
| Media Files | End Users | Never committed to Git | Cloud Object Store (AWS S3) | avatars/user_42.jpg, doc.pdf |
2. Configuring django-storages with AWS S3
Configure AWS credentials and custom storage backends:
3. Custom Storage Backends (storage_backends.py)
Isolate static assets and media files into separate folders inside the same S3 bucket:
4. Handling File Uploads in Models
In templates, access the public S3 URL via .url:
Practice Quiz
Environment Variables & Secrets
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Deploying to Heroku | Environment Variables & Secrets |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.