รูปแบบโฟลเดอร์ static
ตำแหน่งที่ Flask ค้นหาแอสเซ็ตโดยค่าเริ่มต้น
รูปแบบโฟลเดอร์ static เป็นบทเรียน Flask Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Flask Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Assets Need a Home
Your app needs CSS, JavaScript, and images. Flask gives them one default home: a folder named static next to your app. 📁
Where Flask Looks
By convention, Flask serves anything inside the static folder automatically, so you never write a route to hand out a stylesheet yourself.
The Folder Layout
Place static right beside your app.py file. Flask finds it relative to your application package without any extra setup.
myapp/
app.py
static/
style.css
templates/The Public URL Prefix
Files in that folder are reachable under the /static URL prefix. A file named style.css becomes available at /static/style.css in the browser.
Organize with Subfolders
You can nest folders inside static to stay tidy. The path on disk simply maps to the path in the URL, one to one.
static/
css/style.css
js/app.js
img/logo.pngURL Mirrors the Path
So static/css/style.css is served at /static/css/style.css. The browser path mirrors your folder structure exactly. 🔗
It Is Just a Default
The name static is a default, not a law. Flask uses it unless you tell it otherwise, which keeps every project predictable.
Rename the Folder if Needed
Pass static_folder when you create the app to point Flask at a differently named directory on disk.
from flask import Flask
app = Flask(__name__, static_folder="assets")Change the URL Prefix Too
You can also change the public prefix with static_url_path, so assets appear under /files instead of /static.
app = Flask(__name__, static_url_path="/files")Static Means Unchanging
These files are static because Flask sends them as-is, with no Python running. That is exactly why serving them is so fast.
Templates Are Different
Do not put HTML you render into static. Dynamic pages belong in templates, while static holds only finished, ready-to-send assets.
Quick Check
You drop logo.png into the static folder. Where can the browser reach it?
Recap: The static Folder
You learned the static convention: drop assets in that folder and Flask serves them under /static, fast and free. Next, link them cleanly. ✨
คำถามที่พบบ่อย
บทเรียน “รูปแบบโฟลเดอร์ static” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “รูปแบบโฟลเดอร์ static” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flask Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “รูปแบบโฟลเดอร์ static”
ตำแหน่งที่ Flask ค้นหาแอสเซ็ตโดยค่าเริ่มต้น คุณปฏิบัติ Flask Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flask Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flask Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “รูปแบบโฟลเดอร์ static” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Flask Academy นี้ได้ไหม
ได้ บทเรียน Flask Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- รูปแบบโฟลเดอร์ static
- เชื่อมโยงแอสเซ็ตด้วย url_for static
- ป้องกันแคชด้วยเวอร์ชันไฟล์
- ความเป็นจริงของไฟล์สแตติกในการใช้งานจริง