MEDIA_ROOT und Datei-Uploads
Von Nutzern hochgeladene Dateien annehmen und speichern
MEDIA_ROOT und Datei-Uploads ist eine kostenlose Django Academy-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Django Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Django Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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. 📁
Häufig gestellte Fragen
Ist die Lektion „MEDIA_ROOT und Datei-Uploads“ kostenlos?
Ja — der vollständige Text von „MEDIA_ROOT und Datei-Uploads“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Django Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Django Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „MEDIA_ROOT und Datei-Uploads“?
Von Nutzern hochgeladene Dateien annehmen und speichern Du übst Django Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Django Academy zu starten?
Keine Vorkenntnisse erforderlich. Django Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.
Wie lange dauert die Lektion „MEDIA_ROOT und Datei-Uploads“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Django Academy-Lektion Code schreiben und ausführen?
Ja. Jede Django Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- STATICFILES und der static-Tag
- collectstatic für Production
- MEDIA_ROOT und Datei-Uploads
- ImageField und Uploads bereitstellen