collectstatic untuk Produksi
Kumpulkan berkas statis untuk penerapan
collectstatic untuk Produksi adalah pelajaran Django Academy gratis di CoddyKit. Ini adalah pelajaran 2 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Django Academy, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Django Academy mencakup 4 pelajaran total.
Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.
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. 🚀
Pertanyaan yang Sering Diajukan
Apakah pelajaran “collectstatic untuk Produksi” gratis?
Ya — teks lengkap “collectstatic untuk Produksi” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Django Academy, upgrade ke CoddyKit PRO. Kursus Django Academy mencakup 4 pelajaran total.
Apa yang akan aku pelajari di “collectstatic untuk Produksi”?
Kumpulkan berkas statis untuk penerapan Kamu berlatih Django Academy dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.
Apakah aku perlu pengalaman untuk memulai Django Academy?
Tidak diperlukan pengalaman sebelumnya. Django Academy di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 2 dari 4.
Berapa lama pelajaran “collectstatic untuk Produksi” memakan waktu?
Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.
Bisakah aku menulis dan menjalankan kode dalam pelajaran Django Academy ini?
Ya. Setiap pelajaran Django Academy menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.
Semua pelajaran dalam kursus ini
- STATICFILES dan Tag static
- collectstatic untuk Produksi
- MEDIA_ROOT dan Pengunggahan Berkas
- ImageField dan Menyajikan Unggahan