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

ดึงตัวแปรจากเส้นทาง

ใช้พารามิเตอร์ในวงเล็บแหลมกับเส้นทาง

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

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

Static Routes Hit a Wall

A fixed path like /user/alice only works for one person. To serve any name, you need a route that captures part of the URL as data.

Angle Brackets Capture Values

Wrap a path segment in angle brackets to turn it into a variable. Flask grabs whatever the user types there and hands it to your view as a parameter. 🎯

@app.route('/user/<name>')
def profile(name):
    return f'Hello, {name}!'

The Name Must Match

The word inside the brackets becomes the function argument. The name in the URL and the parameter in your view function must be spelled identically.

@app.route('/city/<place>')
def show(place):
    return place

Values Arrive as Strings

By default a captured value is always a Python string. Visiting /user/42 gives you the text "42", not the number 42.

@app.route('/id/<value>')
def show(value):
    return type(value).__name__

Capture More Than One

A single route can capture several segments. Each set of angle brackets becomes its own argument in the view function, in order.

@app.route('/<category>/<item>')
def show(category, item):
    return f'{category}: {item}'

Mix Fixed and Dynamic Parts

You can blend static text with captured pieces. Only the bracketed segment is dynamic; the rest of the path stays fixed and literal.

@app.route('/posts/<slug>/edit')
def edit(slug):
    return slug

Use the Value in Your Logic

Once captured, the variable is just normal Python. Look it up in a database, format it, or branch on it like any other value.

@app.route('/user/<name>')
def hi(name):
    return f'Welcome back, {name.title()}'

Each Request Is Independent

Every visit fills the captured variable fresh. One user hitting /user/sam and another hitting /user/lee get separate, isolated requests.

Slashes Split Segments

A normal capture stops at the next slash. So name in /user/<name> will not include any /, since the slash marks the end of that segment.

Naming Your Variables Well

Pick clear names that describe the data, like <username> or <product_id>. Good names make routes self-documenting and easier to maintain.

A Tiny Real Example

Captured variables power friendly URLs like /greet/Maria. The view simply uses the value to build a personalized response for each visitor.

@app.route('/greet/<who>')
def greet(who):
    return f'Nice to meet you, {who}'

Quick Check

You wrote /user/<name> with def profile(name). What gets passed to name?

Recap: You Captured the Path

Angle brackets turn part of a URL into a function argument. Captured values arrive as strings, and you can grab several at once. Next: typing them. 🚀

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

บทเรียน “ดึงตัวแปรจากเส้นทาง” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “ดึงตัวแปรจากเส้นทาง”

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

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

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

บทเรียน “ดึงตัวแปรจากเส้นทาง” ใช้เวลานานแค่ไหน

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

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

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

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

  1. ดึงตัวแปรจากเส้นทาง
  2. ตัวแปลงชนิด: int, float, string, path
  3. สร้าง URL ด้วย url_for
  4. เครื่องหมายทับท้ายและพฤติกรรมการเปลี่ยนเส้นทาง
← กลับไปที่ Flask Academy