ImageField and FileField
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.
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.
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.
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. #}
- Using ImageField without Pillow installed — Django raises an error since ImageField depends on it to validate images.
- Forgetting the upload_to argument, which leaves uploaded files with no organized storage subdirectory.
- Treating the field's database value as file content — it only stores a path string; the file itself lives on disk under MEDIA_ROOT.
- 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.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: