0
0
DjangoComparisonBeginner · 4 min read

Static vs Media Files in Django: Key Differences and Usage

In Django, static files are assets like CSS, JavaScript, and images used for site design and functionality, while media files are user-uploaded content like photos or documents. Static files are managed by Django's staticfiles app, and media files require separate handling with MEDIA_URL and MEDIA_ROOT settings.
⚖️

Quick Comparison

This table summarizes the main differences between static and media files in Django.

AspectStatic FilesMedia Files
PurposeSite assets like CSS, JS, imagesUser-uploaded content like photos, documents
Managed byDjango's staticfiles appCustom handling via MEDIA settings
Settings usedSTATIC_URL, STATIC_ROOTMEDIA_URL, MEDIA_ROOT
Typical locationProject's static directoriesSeparate media directory
Access during developmentServed automatically by DjangoServed via special URL patterns
Example filesstyle.css, script.js, logo.pngprofile_pic.jpg, uploaded_resume.pdf
⚖️

Key Differences

Static files are files that your website needs to display its design and behavior. These include CSS files for styling, JavaScript files for interactivity, and images like logos or icons. They are usually part of your project or app and do not change often. Django collects these files into a single location for easy serving, especially in production.

Media files are files uploaded by users, such as profile pictures, documents, or any content users add to your site. These files are not part of your codebase and can change frequently. Django does not manage them automatically; you must configure MEDIA_URL and MEDIA_ROOT to tell Django where to store and serve these files.

In development, Django can serve both static and media files, but in production, static files are usually served by a web server or CDN, while media files require special handling to ensure security and accessibility.

💻

Static Files Code Example

python
from django.conf import settings
from django.conf.urls.static import static
from django.urls import path
from django.http import HttpResponse

def home(request):
    return HttpResponse('<html><head><link rel="stylesheet" href="/static/style.css"></head><body><h1>Hello Static</h1></body></html>')

urlpatterns = [
    path('', home),
]

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATICFILES_DIRS[0])
Output
A webpage showing 'Hello Static' styled by style.css loaded from the static directory.
↔️

Media Files Equivalent

python
from django.conf import settings
from django.conf.urls.static import static
from django.urls import path
from django.http import HttpResponse

def home(request):
    return HttpResponse('<html><body><h1>User Image</h1><img src="/media/user_photo.jpg" alt="User Photo"></body></html>')

urlpatterns = [
    path('', home),
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Output
A webpage showing 'User Image' with an image loaded from the media directory.
🎯

When to Use Which

Choose static files for all your website's design and functionality assets that do not change based on user input, like CSS, JavaScript, and fixed images. Use media files when you need to handle user-uploaded content such as profile pictures, documents, or any files users add to your site. Properly separating these helps keep your project organized and ensures efficient file serving and security.

Key Takeaways

Static files are fixed assets like CSS and JS; media files are user-uploaded content.
Configure STATIC_URL and STATIC_ROOT for static files, MEDIA_URL and MEDIA_ROOT for media files.
Django serves static files automatically in development; media files need explicit URL patterns.
Use static files for site design; use media files for dynamic user content.
Separating static and media files improves project organization and deployment.