0Pricing
Django Academy · درس

WhiteNoise للملفات الثابتة

تضمين الأصول الثابتة دون استضافة منفصلة

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

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

The Static File Problem

In production Django will not serve your CSS and JS for you. Normally you need Nginx or a CDN, which adds setup. WhiteNoise simplifies this. 📦

What WhiteNoise Does

WhiteNoise lets your Django app serve its own static files efficiently, so a simple Gunicorn-only deploy can ship CSS and JS too.

Great for Simple Hosts

On platforms like Heroku or small containers where there is no separate web server for files, WhiteNoise keeps everything in one process.

Install WhiteNoise

Add the package with pip, then pin it in requirements.txt so your deploys stay reproducible. WhiteNoise needs no extra services.

pip install whitenoise

Add the Middleware

Insert the WhiteNoiseMiddleware right after SecurityMiddleware. Order matters, since it intercepts static requests early in the chain.

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
]

Set the Storage Backend

Point STATICFILES_STORAGE at WhiteNoise to enable compression and cache-friendly hashed filenames automatically.

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

Define STATIC_ROOT

WhiteNoise serves whatever collectstatic gathers, so STATIC_ROOT must point to the folder where those files land.

STATIC_ROOT = BASE_DIR / 'staticfiles'

Run collectstatic

Before serving, run collectstatic to copy every app's assets into STATIC_ROOT. WhiteNoise reads from that single folder.

python manage.py collectstatic --noinput

Compression Comes Free

The compressed storage backend pre-builds gzip and brotli versions of your files, so browsers download smaller, faster assets.

Far-Future Caching

WhiteNoise adds a content hash to each filename and sets long cache headers, so browsers safely reuse files until they truly change.

WhiteNoise vs Nginx

WhiteNoise wins on simplicity, but a dedicated Nginx or CDN is faster at huge scale. Pick based on your traffic and hosting setup.

Quick Check

Where do you register WhiteNoise in your project?

Recap

You enabled WhiteNoise: install it, add the middleware, set compressed storage, run collectstatic, and your app serves cached static files itself. 🎯

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

هل درس «WhiteNoise للملفات الثابتة» مجاني؟

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

ماذا ستتعلم في «WhiteNoise للملفات الثابتة»؟

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

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

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

كم من الوقت يستغرق درس «WhiteNoise للملفات الثابتة»؟

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

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

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

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

  1. Gunicorn كخادم WSGI
  2. Nginx كوكيل عكسي
  3. WhiteNoise للملفات الثابتة
  4. متغيرات البيئة وتقسيم الإعدادات
← العودة إلى Django Academy