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

สร้าง URL ด้วย url_for

สร้างลิงก์จากชื่อปลายทางแทนเส้นทางที่เขียนตายตัว

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

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

Hardcoded URLs Are Fragile

Typing "/user/alice" by hand breaks the day you rename a route. Flask offers url_for to build links from your view names instead.

Build by Endpoint Name

url_for takes the view function name and returns its path. Change the route string later and every generated link updates automatically. 🔗

from flask import url_for
url_for('home')

The Endpoint Is the Function Name

By default the endpoint is the view function's name, not its URL path. So def home() is referenced as url_for('home').

@app.route('/')
def home():
    return 'Hi'

Pass Dynamic Values

For routes with captured parts, supply them as keyword arguments. url_for drops each value into the matching angle-bracket slot.

url_for('profile', name='alice')
# -> '/user/alice'

Extra Args Become Query Strings

Arguments that do not fit a route slot are added as a query string. So passing page=2 appends ?page=2 to the generated URL.

url_for('search', q='cats', page=2)
# -> '/search?q=cats&page=2'

Use It Inside Templates

Templates call url_for too, so your HTML links never go stale. It is the standard way to write hrefs in Jinja2 pages.

<a href="{{ url_for('home') }}">Home</a>

Refactor Without Fear

Because links derive from endpoint names, you can rename a URL path freely. Every url_for call keeps working, so refactors stay safe.

Redirect Pairs Well with It

Combine url_for with redirect to send users to a named view. You get a clean, refactor-proof redirect without writing the path by hand.

from flask import redirect
redirect(url_for('home'))

Absolute URLs When Needed

Pass _external=True to get a full http://host/path link. This is handy for emails or anywhere a relative path will not do.

url_for('home', _external=True)

It Knows the static Folder

url_for also builds links to assets via the static endpoint. That keeps CSS and image paths correct even if your app mounts elsewhere.

url_for('static', filename='app.css')

A Habit Worth Forming

Make url_for your default for every internal link. Hardcoded strings creep in and rot; generated URLs stay correct as your app evolves.

Quick Check

You call url_for('profile', name='alice', page=2). Where does page=2 end up?

Recap: You Built URLs Smartly

url_for turns endpoint names into paths, fills route variables, and tacks extras onto the query string. Next: trailing slashes. 🧩

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

บทเรียน “สร้าง URL ด้วย url_for” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “สร้าง URL ด้วย url_for”

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

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

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

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

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

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

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

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

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