0Pricing
Django Academy · บทเรียน

WhiteNoise สำหรับไฟล์สแตติก

นำส่งทรัพยากรสแตติกโดยไม่ต้องโฮสต์แยก

WhiteNoise สำหรับไฟล์สแตติก เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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 สำหรับไฟล์สแตติก” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “WhiteNoise สำหรับไฟล์สแตติก”

นำส่งทรัพยากรสแตติกโดยไม่ต้องโฮสต์แยก คุณปฏิบัติ Django Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Django Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Django Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “WhiteNoise สำหรับไฟล์สแตติก” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Django Academy นี้ได้ไหม

ได้ บทเรียน Django Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. Gunicorn ในฐานะเซิร์ฟเวอร์ WSGI
  2. Nginx ในฐานะพร็อกซีย้อนกลับ
  3. WhiteNoise สำหรับไฟล์สแตติก
  4. ตัวแปรสภาพแวดล้อมและการแยกการตั้งค่า
← กลับไปที่ Django Academy