0Pricing
Django Academy · درس

ImageField وتقديم الملفات المرفوعة

معالجة الصور والربط بالوسائط في بيئة التطوير

ImageField وتقديم الملفات المرفوعة درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Django Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Django Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

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 Pillow

Validation 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. 🎉

الأسئلة الشائعة

هل درس «ImageField وتقديم الملفات المرفوعة» مجاني؟

نعم — نص درس «ImageField وتقديم الملفات المرفوعة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Django Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Django Academy 4 دروس في المجموع.

ماذا ستتعلم في «ImageField وتقديم الملفات المرفوعة»؟

معالجة الصور والربط بالوسائط في بيئة التطوير تتمرن على Django Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Django Academy؟

لا تُشترط خبرة سابقة. Django Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «ImageField وتقديم الملفات المرفوعة»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Django Academy هذا؟

نعم. كل درس في Django Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. STATICFILES ووسم static
  2. collectstatic لبيئة Production
  3. MEDIA_ROOT وعمليات رفع الملفات
  4. ImageField وتقديم الملفات المرفوعة
← العودة إلى Django Academy