0Pricing
Flask Academy · บทเรียน

สร้างและลงทะเบียน Blueprint

กำหนด Blueprint และเชื่อมเข้ากับแอป

สร้างและลงทะเบียน Blueprint เป็นบทเรียน Flask Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Flask Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Two Steps to a Blueprint

Working with a blueprint is two moves: first you define it in its own module, then you register it on the app. Let us walk through both.

Import the Blueprint Class

Everything starts with one import. The Blueprint class comes straight from flask, just like the Flask app object does.

from flask import Blueprint

Create the Blueprint Object

You build a blueprint by giving it a name and import_name. The name uniquely identifies this blueprint inside your whole app.

blog = Blueprint("blog", __name__)

What __name__ Does

That second argument, __name__, tells the blueprint where it lives so Flask can later find its templates and static files.

Routes on the Blueprint

Inside the module you attach views with @blog.route instead of @app.route. Same idea, but bound to the blueprint.

@blog.route("/posts")
def posts():
    return "All posts"

Still Dormant for Now

Defining routes is not enough. Until you register the blueprint, none of its URLs respond. The blueprint is inert on its own.

Import It in the App

Over in your app setup, you import the blueprint object from its module so the app can take ownership of it.

from blog.routes import blog

Register on the App

One call wires everything up. register_blueprint copies the blueprint routes onto the app and makes them live.

app.register_blueprint(blog)

Order of Operations

Create the app, import the blueprint, then register it. Get this sequence right and your feature routes spring to life. ✅

Endpoint Names Get Namespaced

A view named posts inside the blog blueprint is referenced as blog.posts. The blueprint name becomes a tidy prefix for endpoints.

Avoiding Circular Imports

Keep the blueprint in its own file and import it where you build the app. This structure sidesteps painful circular import errors.

Quick Check

Which call actually makes a blueprint active?

Recap

Define a Blueprint with a name and __name__, add routes with @bp.route, then call register_blueprint on the app to make them live. 🎉

คำถามที่พบบ่อย

บทเรียน “สร้างและลงทะเบียน Blueprint” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “สร้างและลงทะเบียน Blueprint” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flask Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “สร้างและลงทะเบียน Blueprint”

กำหนด Blueprint และเชื่อมเข้ากับแอป คุณปฏิบัติ Flask Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flask Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flask Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “สร้างและลงทะเบียน Blueprint” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Flask Academy นี้ได้ไหม

ได้ บทเรียน Flask Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. เหตุใด Blueprints จึงดีกว่าไฟล์ยักษ์ไฟล์เดียว
  2. สร้างและลงทะเบียน Blueprint
  3. คำนำหน้า URL และ url_for ของ Blueprint
  4. เทมเพลตและไฟล์สแตติกที่อยู่ในขอบเขต Blueprint
← กลับไปที่ Flask Academy