Static vs Media Files in Django: Key Differences and Usage
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.
| Aspect | Static Files | Media Files |
|---|---|---|
| Purpose | Site assets like CSS, JS, images | User-uploaded content like photos, documents |
| Managed by | Django's staticfiles app | Custom handling via MEDIA settings |
| Settings used | STATIC_URL, STATIC_ROOT | MEDIA_URL, MEDIA_ROOT |
| Typical location | Project's static directories | Separate media directory |
| Access during development | Served automatically by Django | Served via special URL patterns |
| Example files | style.css, script.js, logo.png | profile_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
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])
Media Files Equivalent
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)
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_URL and STATIC_ROOT for static files, MEDIA_URL and MEDIA_ROOT for media files.