Django Media Files

In Django, media files are files uploaded by users during runtime, such as profile pictures, resumes, PDFs, videos, and other documents.

Unlike static files, which are part of your application, media files are created and managed by users.

Static Files vs Media Files

Static FilesMedia Files
Managed by developersUploaded by users
CSS, JavaScript, ImagesProfile pictures, PDFs, Videos
Stored in static directoryStored in media directory
Collected using collectstaticStored directly in MEDIA_ROOT

Configure Media Files

Open settings.py and add:

MEDIA_URL = "/media/"
MEDIA_ROOT = BASE_DIR / "media"
  • MEDIA_URL defines the URL used to access uploaded files.
  • MEDIA_ROOT defines the location where uploaded files are stored.

Configure URLs

Open urls.py:

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # Your URLs
]

if settings.DEBUG:
    urlpatterns += static(
        settings.MEDIA_URL,
        document_root=settings.MEDIA_ROOT
    )

This allows Django to serve media files during development.

Creating a Model with ImageField

from django.db import models

class Profile(models.Model):
    name = models.CharField(max_length=100)
    profile_picture = models.ImageField(
        upload_to="profiles/"
    )

    def __str__(self):
        return self.name

The upload_to parameter specifies where uploaded files will be stored inside the media directory.

Creating a Model with FileField

from django.db import models

class Resume(models.Model):
    name = models.CharField(max_length=100)
    file = models.FileField(
        upload_to="resumes/"
    )

    def __str__(self):
        return self.name

FileField can store any file type such as PDFs, Word documents, ZIP files, and more.

Install Pillow for ImageField

To use ImageField, install Pillow:

pip install pillow

Pillow enables Django to process image uploads.

Access Uploaded Files

After uploading a file, Django automatically provides the file URL:

profile.profile_picture.url

Example output:

/media/profiles/profile.jpg

Display Images in Templates

<img src="{{ profile.profile_picture.url }}" alt="Profile Picture">

This renders the uploaded image in the browser.

Organizing Uploads

You can organize files using subdirectories:

profile_picture = models.ImageField(
    upload_to="users/profile_pictures/"
)

Uploaded files will be stored like:

media/
└── users/
    └── profile_pictures/
        └── image.jpg

Common Errors

ImageField requires Pillow

Cannot use ImageField because Pillow is not installed

Solution:

pip install pillow

Media files are not loading

Check:

MEDIA_URL
MEDIA_ROOT

and ensure media URLs are configured in urls.py.

File does not exist

Make sure the file was uploaded successfully and exists inside the media directory.

Best Practices

  • Use ImageField for image uploads.
  • Use FileField For documents and other file types.
  • Organize uploads using upload_to.
  • Validate file size and file type before saving.
  • Store media files on cloud storage such as AWS S3 in production environments.
  • Never store sensitive files in publicly accessible directories.