Production向けのcollectstatic
デプロイ用に静的ファイルを集めます
「Production向けのcollectstatic」はCoddyKit上の無料Django Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはDjango Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Django Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Dev vs Production
In development Django finds and serves static files for you. In production that auto-serving is off, so you must gather assets into one place first.
Meet collectstatic
The collectstatic command copies every static file from every app into a single output folder, ready for a web server to serve fast.
python manage.py collectstaticThe STATIC_ROOT Target
collectstatic needs a destination. The STATIC_ROOT setting points to the single folder where all collected files will be written.
STATIC_ROOT = BASE_DIR / 'staticfiles'Keep Roots Separate
Never set STATIC_ROOT to a folder you edit by hand. It is a generated output directory, rebuilt on every collectstatic run.
What It Gathers
collectstatic pulls from each app's static folder and from STATICFILES_DIRS, merging them all into STATIC_ROOT in one pass.
Skip the Prompt
By default it asks before overwriting. In a deploy script add --noinput so it runs unattended without waiting for you.
python manage.py collectstatic --noinputWhen to Run It
Run collectstatic on every deploy, after pulling new code. New or changed CSS and JS only reach users once they are collected.
Cache Busting
The ManifestStaticFilesStorage backend adds a content hash to each filename, so browsers always fetch the newest version after a deploy.
STATICFILES_STORAGE = (
'django.contrib.staticfiles.storage'
'.ManifestStaticFilesStorage')Who Serves Them
Django itself does not serve STATIC_ROOT in production. A web server like Nginx or a tool like WhiteNoise delivers those files instead.
Ignore Build Junk
Add --ignore patterns to skip files you do not want copied, like source maps or scratch files left in a static folder.
python manage.py collectstatic --ignore=*.scssGit and STATIC_ROOT
Since STATIC_ROOT is generated, add it to .gitignore. You commit your source assets, not the collected copies.
Quick Check
Let us confirm the role of collectstatic.
Recap
You saw that production needs collectstatic to merge assets into STATIC_ROOT, that you run it each deploy, and that a web server serves the result. 🚀
よくある質問
「Production向けのcollectstatic」レッスンは無料ですか?
はい。「Production向けのcollectstatic」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Django Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Django Academyコースには全4レッスンが含まれています。
「Production向けのcollectstatic」で何を学びますか?
デプロイ用に静的ファイルを集めます ブラウザで直接実行するハンズオンコードでDjango Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Django Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのDjango Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「Production向けのcollectstatic」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このDjango Academyレッスンでコードを書いて実行できますか?
はい。すべてのDjango Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- STATICFILESとstaticタグ
- Production向けのcollectstatic
- MEDIA_ROOTとファイルアップロード
- ImageFieldとアップロードの配信