collectstatic สำหรับใช้งานจริง
รวบรวมไฟล์สแตติกสำหรับการนำไปใช้งาน
collectstatic สำหรับใช้งานจริง เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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. 🚀
คำถามที่พบบ่อย
บทเรียน “collectstatic สำหรับใช้งานจริง” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “collectstatic สำหรับใช้งานจริง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “collectstatic สำหรับใช้งานจริง”
รวบรวมไฟล์สแตติกสำหรับการนำไปใช้งาน คุณปฏิบัติ Django Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Django Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Django Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “collectstatic สำหรับใช้งานจริง” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Django Academy นี้ได้ไหม
ได้ บทเรียน Django Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- STATICFILES และแท็ก static
- collectstatic สำหรับใช้งานจริง
- MEDIA_ROOT และการอัปโหลดไฟล์
- ImageField และการให้บริการไฟล์อัปโหลด