MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Static & Media Files0%
Study Materials
Django for Backend DevelopmentProgress 0%
1.1Installing Django & Environment Setup1.2Django Project Structure1.3Creating First Django App
10.1Deploying to Heroku10.2Static & Media Files10.3Environment Variables & Secrets
HomeTutorialsDjango for Backend DevelopmentStatic & Media Files
Environment Variables & Secrets

Static & Media Files

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

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

CategoryAuthored BySource ControlStorage in ProductionExample
Static FilesDevelopersCommitted to GitWhiteNoise or AWS S3 / CDNcss/styles.css, bundle.js
Media FilesEnd UsersNever committed to GitCloud Object Store (AWS S3)avatars/user_42.jpg, doc.pdf

2. Configuring django-storages with AWS S3

1pip install django-storages boto3

Configure AWS credentials and custom storage backends:

1# config/settings.py
2INSTALLED_APPS = [
3 # ...
4 "storages", # Adds S3 backend support
5]
6 
7# AWS S3 Credentials (Injected from Environment Variables)
8AWS_ACCESS_KEY_ID = os.environ.get("AWS_ACCESS_KEY_ID")
9AWS_SECRET_ACCESS_KEY = os.environ.get("AWS_SECRET_ACCESS_KEY")
10AWS_STORAGE_BUCKET_NAME = os.environ.get("AWS_STORAGE_BUCKET_NAME")
11AWS_S3_REGION_NAME = os.environ.get("AWS_S3_REGION_NAME", "us-east-1")
12AWS_S3_CUSTOM_DOMAIN = f"{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com"
13 
14# S3 Security & Performance Options
15AWS_DEFAULT_ACL = None # Enforces AWS bucket ownership controls
16AWS_S3_OBJECT_PARAMETERS = {
17 "CacheControl": "max-age=86400",
18}
19 
20# 1. Custom Media Storage Backend (User Uploads)
21DEFAULT_FILE_STORAGE = "config.storage_backends.MediaStorage"
22 
23# 2. Custom Static Storage Backend (Optional: offload static to S3)
24STATICFILES_STORAGE = "config.storage_backends.StaticStorage"

3. Custom Storage Backends (storage_backends.py)

Isolate static assets and media files into separate folders inside the same S3 bucket:

1# config/storage_backends.py
2from storages.backends.s3boto3 import S3Boto3Storage
3 
4class StaticStorage(S3Boto3Storage):
5 location = "static"
6 default_acl = "public-read"
7 
8class MediaStorage(S3Boto3Storage):
9 location = "media"
10 file_overwrite = False # Appends random suffix if filename conflicts!

4. Handling File Uploads in Models

1# accounts/models.py
2from django.db import models
3 
4def user_avatar_path(instance, filename):
5 # Dynamic storage path: media/avatars/user_123/profile.jpg
6 return f"avatars/user_{instance.id}/{filename}"
7 
8class UserProfile(models.Model):
9 user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
10 avatar = models.ImageField(upload_to=user_avatar_path, blank=True, null=True)
11 resume = models.FileField(upload_to="resumes/", blank=True, null=True)

In templates, access the public S3 URL via .url:

1{% if profile.avatar %}
2 <img src="{{ profile.avatar.url }}" alt="Profile Photo" class="h-12 w-12 rounded-full" />
3{% endif %}

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What is the critical difference between Static Files and Media Files in Django?
Question 2
Why is storing user uploads on local container disks an anti-pattern in Docker / Heroku deployments?
Question 3
What does setting file_overwrite = False do in custom S3 media storage?
Question 4
What package is the standard backend for integrating Django with cloud storage providers like AWS S3, Azure Blob, and Google Cloud Storage?
Question 5
How do you output the public URL of an uploaded ImageField named avatar in a Django template?
Next Lesson

Environment Variables & Secrets

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Deploying to HerokuEnvironment Variables & Secrets

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master Django for Backend Development with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext