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

ตัวแปลงชนิด: int, float, string, path

จำกัดส่วนของ URL ด้วยตัวแปลงในตัว

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

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

Strings Are Not Always Enough

Captured values default to strings, but /post/42 should give a number, not text. A converter tells Flask what type to expect in that segment.

Converter Syntax

Put the converter name before the variable with a colon. The pattern is <converter:name>, and Flask applies it before calling your view.

@app.route('/post/<int:post_id>')
def show(post_id):
    return str(post_id + 1)

The int Converter

Use int to capture whole numbers. /post/42 hands your view the integer 42, so you can do math without calling int() yourself. 🔢

@app.route('/page/<int:n>')
def page(n):
    return f'Page {n}'

int Rejects Non-Numbers

A converter also filters URLs. /page/abc will not match an int route, so Flask returns 404 instead of crashing inside your function.

The float Converter

Use float for decimal numbers like prices or ratings. /rate/4.5 gives you the float 4.5, ready for arithmetic right away.

@app.route('/rate/<float:score>')
def rate(score):
    return str(score * 2)

The string Converter

string is the default: it accepts any text but stops at a slash. You rarely write it explicitly since plain <name> already behaves this way.

@app.route('/tag/<string:label>')
def tag(label):
    return label

The path Converter

Use path when the value can contain slashes, like a folder route. Unlike string, it captures /docs/intro/setup as one whole value.

@app.route('/files/<path:filepath>')
def serve(filepath):
    return filepath

Pick the Right Tool

Match the converter to your data: int for ids, float for decimals, string for words, path for slashed routes. The right choice prevents bad input early.

Converters Validate for You

By rejecting mismatched URLs with a 404, converters act as a first line of validation. Your view only runs when the type already fits.

Combine in One Route

You can mix converters across segments. Here an int id and a string tab live in the same path, each typed independently.

@app.route('/user/<int:uid>/<tab>')
def view(uid, tab):
    return f'{uid}:{tab}'

Negatives and Edge Cases

The built-in int converter matches only non-negative whole numbers by default. For minus signs or stricter rules, you would write a custom converter later.

Quick Check

Which converter lets a captured value include slashes, like docs/intro/setup?

Recap: You Typed Your URLs

Converters give captured values a type and reject bad input with a 404. Remember int, float, string, and path. Next: building URLs safely. 🧭

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

บทเรียน “ตัวแปลงชนิด: int, float, string, path” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “ตัวแปลงชนิด: int, float, string, path”

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

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

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

บทเรียน “ตัวแปลงชนิด: int, float, string, path” ใช้เวลานานแค่ไหน

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

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

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

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

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