静的ファイル用のWhiteNoise
別のホスティングなしで静的アセットを配信します
「静的ファイル用のWhiteNoise」はCoddyKit上の無料Django Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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 whitenoiseAdd 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 --noinputCompression 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時間対応のAIチューター)、Django Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Django Academyコースには全4レッスンが含まれています。
「静的ファイル用のWhiteNoise」で何を学びますか?
別のホスティングなしで静的アセットを配信します ブラウザで直接実行するハンズオンコードでDjango Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Django Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのDjango Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「静的ファイル用のWhiteNoise」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このDjango Academyレッスンでコードを書いて実行できますか?
はい。すべてのDjango Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。