MEDIA_ROOT and File Uploads
Accept and store user-uploaded files.
MEDIA_ROOT and File Uploads is a free Django Academy lesson on CoddyKit — lesson 3 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.
Static vs Media
Static files ship with your code. Media files are uploaded by your users at runtime, like avatars or documents, and they need their own home.
The MEDIA_ROOT Setting
MEDIA_ROOT is the absolute folder on disk where Django saves every uploaded file. It lives outside your source code tree.
MEDIA_ROOT = BASE_DIR / 'media'The MEDIA_URL Setting
MEDIA_URL is the public prefix browsers use to request an uploaded file, just like STATIC_URL does for static assets.
MEDIA_URL = '/media/'Add a FileField
To accept an upload, give your model a FileField. Django stores the file on disk and keeps only its path in the database.
class Resume(models.Model):
doc = models.FileField(upload_to='resumes/')The upload_to Path
The upload_to argument is a subfolder under MEDIA_ROOT. Use it to keep uploads tidy, like grouping all resumes together.
Dynamic upload_to
Pass a function to upload_to when the path should depend on the instance, such as foldering files per user.
def user_dir(instance, filename):
return f'user_{instance.user.id}/{filename}'Forms Need enctype
An HTML form that uploads files must set enctype to multipart/form-data, or the browser sends no file bytes at all.
<form method="post" enctype="multipart/form-data">Read request.FILES
Uploaded bytes do not arrive in request.POST. They land in request.FILES, which you pass into your form to bind them.
form = ResumeForm(request.POST, request.FILES)Get the URL
Once saved, every file field exposes a .url property that builds the MEDIA_URL link you drop into a template.
<a href="{{ resume.doc.url }}">Download</a>Validate Uploads
Never trust raw uploads. Limit file size and type in your form's clean method to keep malicious files out. 🔒
Keep Media Private
MEDIA_ROOT should sit outside your repo and never be served by collectstatic. User files and code stay clearly separate.
Quick Check
Let us check where uploaded file data arrives in a view.
Recap
You learned that MEDIA_ROOT stores user uploads, that FileField with upload_to organizes them, and that request.FILES carries the bytes. 📁
Frequently asked questions
Is the “MEDIA_ROOT and File Uploads” lesson free?
Yes — the full text of “MEDIA_ROOT and File 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 “MEDIA_ROOT and File Uploads”?
Accept and store user-uploaded files. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “MEDIA_ROOT and File 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