MEDIA_ROOT وعمليات رفع الملفات
قبول الملفات التي يرفعها المستخدمون وتخزينها
MEDIA_ROOT وعمليات رفع الملفات درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Django Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Django Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
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. 📁
الأسئلة الشائعة
هل درس «MEDIA_ROOT وعمليات رفع الملفات» مجاني؟
نعم — نص درس «MEDIA_ROOT وعمليات رفع الملفات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Django Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Django Academy 4 دروس في المجموع.
ماذا ستتعلم في «MEDIA_ROOT وعمليات رفع الملفات»؟
قبول الملفات التي يرفعها المستخدمون وتخزينها تتمرن على Django Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Django Academy؟
لا تُشترط خبرة سابقة. Django Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «MEDIA_ROOT وعمليات رفع الملفات»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Django Academy هذا؟
نعم. كل درس في Django Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- STATICFILES ووسم static
- collectstatic لبيئة Production
- MEDIA_ROOT وعمليات رفع الملفات
- ImageField وتقديم الملفات المرفوعة