ImageField and Serving Uploads
Handle images and link to media in dev.
ImageField and Serving Uploads is a free Django Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Django Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why ImageField
For pictures, use ImageField instead of FileField. It works the same but adds image-specific checks and helpers on top.
avatar = models.ImageField(upload_to='avatars/')Pillow Is Required
ImageField depends on the Pillow library to read images. Install it first or migrations will refuse to run. 🖼️
pip install PillowValidation Built In
ImageField verifies an upload is a real image. A renamed .txt file gets rejected during form validation automatically.
Width and Height
Set width_field and height_field and Django records each image's dimensions into those columns on save.
photo = models.ImageField(
upload_to='pics/',
height_field='h', width_field='w')Display the Image
In a template, point an img tag at the field's .url to render the stored picture for the user.
<img src="{{ profile.avatar.url }}" alt="avatar">Guard the URL
Calling .url on an empty field raises an error. Wrap it in an if check so missing images do not crash your page.
{% if profile.avatar %}
<img src="{{ profile.avatar.url }}">
{% endif %}Provide a Default
Give the field a default image path so users without an upload still see a placeholder avatar.
models.ImageField(default='avatars/blank.png')Serving in Dev
Django will not serve media on its own. In development, add a route from MEDIA_URL to MEDIA_ROOT in your project urls.
urlpatterns += static(
settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)Only When DEBUG
That media helper is for development only. In production a real web server serves uploads, so guard the route with a DEBUG check.
if settings.DEBUG:
urlpatterns += static(...)Production Serving
In production let Nginx or a storage service like S3 serve media directly. Routing every image through Django would be slow.
Thumbnails Later
Need resized versions? Libraries like django-imagekit generate thumbnails on top of ImageField when you are ready.
Quick Check
Let us check what ImageField needs to function.
Recap
You learned that ImageField needs Pillow, validates real images, exposes .url for display, and that production should serve media outside Django. 🎉
Frequently asked questions
Is the “ImageField and Serving Uploads” lesson free?
Yes — the full text of “ImageField and Serving Uploads” is free to read here on the web, and the Django Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Django Academy course, upgrade to CoddyKit PRO.
What will I learn in “ImageField and Serving Uploads”?
Handle images and link to media in dev. You practise Django Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Django Academy?
No prior experience is required. Django Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “ImageField and Serving Uploads” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Django Academy lesson?
Yes. Every Django Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- STATICFILES and the static Tag
- collectstatic for Production
- MEDIA_ROOT and File Uploads
- ImageField and Serving Uploads