ตัวตกแต่ง @app.route
ผูกเส้นทาง URL เข้ากับฟังก์ชันมุมมอง
ตัวตกแต่ง @app.route เป็นบทเรียน Flask Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Flask Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What a Route Is
A route connects a URL path to a Python function. When someone visits that path, Flask runs your function and sends back whatever it returns.
Meet the Decorator
You attach a route using @app.route, a decorator written on the line just above your function. It tells Flask which path this function should handle.
@app.route("/")
def home():
return "Welcome!"View Functions
The function below a route is called a view function. Its job is simple: build a response for one specific URL and return it.
The Path Argument
The string you pass, like "/about", is the URL path. It always starts with a slash and tells Flask exactly where this page lives.
@app.route("/about")
def about():
return "About us"The Root Path
A single slash, "/", is the root path. It is your home page, the page people see when they open your site with no extra path.
Returning a Response
Whatever your view function returns becomes the page body. A plain string is the simplest response Flask can send to a browser.
@app.route("/hi")
def hi():
return "Hi there"Why a Decorator?
The @ symbol means decorator. It registers your function with Flask without you calling anything by hand, so Flask knows about the route at startup.
Function Names Are Yours
The function name does not have to match the URL. Pick clear names like contact or profile, since Flask uses them later to build links.
One Decorator, One Path
Each @app.route binds one path to one function. Want another page? Write another decorator and another function below it.
@app.route("/contact")
def contact():
return "Email us"Order Inside the File
You can declare routes in any order in your file. Flask matches by the requested path, not by which function you wrote first.
Paths Are Case Sensitive
Route paths are case sensitive after the host. "/About" and "/about" are different URLs, so keep your paths lowercase and consistent.
Quick Check
Let us make sure the decorator clicked.
Recap
You now wire pages with @app.route: a path string, a view function, and a returned response. That trio is the heart of every Flask app. 🎉
คำถามที่พบบ่อย
บทเรียน “ตัวตกแต่ง @app.route” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ตัวตกแต่ง @app.route” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flask Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ตัวตกแต่ง @app.route”
ผูกเส้นทาง URL เข้ากับฟังก์ชันมุมมอง คุณปฏิบัติ Flask Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flask Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flask Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “ตัวตกแต่ง @app.route” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Flask Academy นี้ได้ไหม
ได้ บทเรียน Flask Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ตัวตกแต่ง @app.route
- คำขอกลายเป็นการตอบกลับได้อย่างไร
- ส่งสตริง HTML และรหัสสถานะ
- หลายเส้นทางในแอปเดียว