เทมเพลตและไฟล์สแตติกที่อยู่ในขอบเขต Blueprint
จัดเตรียมแอสเซ็ตเฉพาะให้แต่ละ Blueprint
เทมเพลตและไฟล์สแตติกที่อยู่ในขอบเขต Blueprint เป็นบทเรียน Flask Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Flask Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Each Feature Owns Its Assets
A truly modular blueprint carries its own templates and static files. This keeps a feature self-contained and easy to move around.
Point to a Template Folder
When you create the blueprint, set template_folder so Flask also searches that folder when rendering this blueprint pages.
blog = Blueprint("blog", __name__,
template_folder="templates")Folders Are Relative
That template path is relative to the blueprint module thanks to __name__. Flask resolves it from where the blueprint file lives.
Templates Are Searched, Not Isolated
Flask merges every template folder into one search path. The blueprint folder is added to the global app templates, not fully walled off.
Avoid Template Name Clashes
Because folders merge, two blueprints with the same filename can collide. Namespace templates by nesting them in a per-blueprint subfolder.
render_template("blog/post.html")Add a Static Folder
A blueprint can ship its own CSS and images too. Set static_folder at creation to hold this feature private assets.
blog = Blueprint("blog", __name__,
static_folder="static")Link Blueprint Static Files
Reference those assets with url_for and the namespaced static endpoint, so each blueprint serves its own files cleanly.
url_for("blog.static", filename="blog.css")Set a Static URL Path
Give the assets a public route with static_url_path. This controls the URL browsers use to fetch the blueprint files.
Blueprint("blog", __name__,
static_url_path="/blog-static")App Static Still Exists
The main app keeps its own /static for shared assets. Blueprint statics live alongside it, each reached by its own endpoint.
A Tidy Blueprint Folder
The payoff is a clean package: routes, templates, and static files in one place. That folder is the whole feature, ready to reuse. 📦
blog/
routes.py
templates/blog/
static/blog.cssPortable by Design
Since assets travel with the code, you can copy a blueprint into another project and it still works with little extra setup. 🚀
Quick Check
How do you link a blueprint static file?
Recap
Set template_folder and static_folder on a blueprint so it owns its assets, namespace templates, and link statics via blog.static. 🎉
คำถามที่พบบ่อย
บทเรียน “เทมเพลตและไฟล์สแตติกที่อยู่ในขอบเขต Blueprint” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “เทมเพลตและไฟล์สแตติกที่อยู่ในขอบเขต Blueprint” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flask Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “เทมเพลตและไฟล์สแตติกที่อยู่ในขอบเขต Blueprint”
จัดเตรียมแอสเซ็ตเฉพาะให้แต่ละ Blueprint คุณปฏิบัติ Flask Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flask Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flask Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “เทมเพลตและไฟล์สแตติกที่อยู่ในขอบเขต Blueprint” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Flask Academy นี้ได้ไหม
ได้ บทเรียน Flask Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เหตุใด Blueprints จึงดีกว่าไฟล์ยักษ์ไฟล์เดียว
- สร้างและลงทะเบียน Blueprint
- คำนำหน้า URL และ url_for ของ Blueprint
- เทมเพลตและไฟล์สแตติกที่อยู่ในขอบเขต Blueprint