หลายเส้นทางในแอปเดียว
เพิ่มหลายหน้าและหลีกเลี่ยงเส้นทางชนกัน
หลายเส้นทางในแอปเดียว เป็นบทเรียน Flask Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Flask Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Many Pages, One App
A real site has many pages. You add each one with its own route and view function, all living inside the same app file.
Stack the Decorators
Just write more @app.route blocks. Each pairs a unique path with its own function, and Flask keeps track of every one.
@app.route("/home")
def home():
return "Home"Add Another Page
Below the first route, add a second for a new path like /about. Two routes, two functions, two pages your users can visit.
@app.route("/about")
def about():
return "About"Each Path Is Unique
Two routes must not share the same path. If they do, Flask cannot tell which view to call and raises an error at startup.
The Collision Error
Reusing a path triggers a collision. Flask warns that the endpoint is already registered, so give every route a distinct URL.
Function Names Must Differ
Each view also needs a unique function name. Two functions named home overwrite each other, and only the last one survives.
Endpoints Behind the Scenes
Flask stores each route under an endpoint name, which defaults to the function name. That name is how Flask refers to the route internally.
Group Related Routes
Keep related pages near each other in the file. Good grouping makes a growing app easier to read and to maintain over time.
Order Does Not Decide Matching
Flask picks a route by the requested path, not by source order. So the position of a route in the file does not change which page loads.
One File Has Limits
Dozens of routes in one file get unwieldy. Later you will split them into blueprints, but for now a single file is perfectly fine.
A Small Multi-Page App
Here is the shape: distinct paths, distinct names, one app. Add a contact route the same way and your site keeps growing.
@app.route("/contact")
def contact():
return "Contact"Quick Check
Spot what causes a route collision.
Recap
You build many pages by stacking routes, each with a unique path and name. Keep them tidy now, and blueprints will scale you later. 🧭
คำถามที่พบบ่อย
บทเรียน “หลายเส้นทางในแอปเดียว” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “หลายเส้นทางในแอปเดียว” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flask Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “หลายเส้นทางในแอปเดียว”
เพิ่มหลายหน้าและหลีกเลี่ยงเส้นทางชนกัน คุณปฏิบัติ Flask Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flask Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flask Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “หลายเส้นทางในแอปเดียว” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Flask Academy นี้ได้ไหม
ได้ บทเรียน Flask Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ตัวตกแต่ง @app.route
- คำขอกลายเป็นการตอบกลับได้อย่างไร
- ส่งสตริง HTML และรหัสสถานะ
- หลายเส้นทางในแอปเดียว