← Back to Django Course | Chapter 11: Static Files & Media Handling | Lesson 7 of 8

ImageField and FileField

ImageField and FileField are like labeled mailboxes on a model — FileField accepts any kind of file, while ImageField only accepts pictures and checks that they're real images.

Defining a FileField

FileField accepts any uploaded file and stores its path relative to MEDIA_ROOT; upload_to organizes uploads into a subdirectory.

Example: Defining a FileField

FileField accepts any uploaded file and stores its path relative to MEDIA_ROOT; upload_to organizes uploads into a subdirectory.

markup
from django.db import models

class Document(models.Model):
    title = models.CharField(max_length=100)
    file = models.FileField(upload_to='documents/')
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Defining an ImageField

ImageField behaves like FileField but additionally validates that the uploaded content is a genuine image, and requires the Pillow package to do so.

Note: Install Pillow with pip before using ImageField, or Django raises an ImproperlyConfigured-style error.

Example: Defining an ImageField

ImageField behaves like FileField but additionally validates that the uploaded content is a genuine image, and requires the Pillow package to do so.

markup
from django.db import models

class Profile(models.Model):
    name = models.CharField(max_length=100)
    avatar = models.ImageField(upload_to='avatars/')
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Accessing the Uploaded File

Both fields expose .url and .path attributes on the saved instance so a template or view can reference the uploaded file.

Example: Accessing the Uploaded File

Both fields expose .url and .path attributes on the saved instance so a template or view can reference the uploaded file.

markup
profile = Profile.objects.get(id=1)
print(profile.avatar.url)
print(profile.avatar.path)
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Common Mistakes
  1. Using ImageField without Pillow installed — Django raises an error since ImageField depends on it to validate images.
  2. Forgetting the upload_to argument, which leaves uploaded files with no organized storage subdirectory.
  3. Treating the field's database value as file content — it only stores a path string; the file itself lives on disk under MEDIA_ROOT.
Chapter Summary
  • FileField accepts any file type; ImageField is a specialized FileField that validates the upload is a real image.
  • ImageField requires the Pillow library to be installed.
  • upload_to sets the subdirectory (relative to MEDIA_ROOT) where uploaded files are stored.
  • Both fields store a file path string in the database, not the file's binary content.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.