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

ฟิลด์แบบฟอร์มผ่าน request.form

ดึงค่าจากแบบฟอร์ม HTML ที่ส่งแล้ว

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

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

Forms Send Data in the Body

When an HTML form posts, the field values travel inside the request body, not the URL, so request.args will not find them.

Meet request.form

For posted form fields Flask gives you request.form, a dict-like object keyed by each input name on the page.

name = request.form["name"]

The name Attribute Is the Key

A field shows up in request.form under its HTML name attribute, so name="email" becomes request.form["email"].

Use .get for Optional Fields

Just like with args, prefer .get so a field the user left out returns None instead of crashing your view with an error.

phone = request.form.get("phone")

Provide a Default

Give .get a fallback and missing optional fields take a clean default, keeping the rest of your logic simple.

role = request.form.get("role", "user")

Match the HTTP Method

Form data only arrives on a POST, so your route must allow POST or the browser gets a 405 before your code runs.

@app.route("/signup", methods=["POST"])

Form Values Are Strings Too

Every form value is text. Convert ages, prices, or counts yourself, and guard against bad input that will not parse.

age = int(request.form.get("age", 0))

Handle Multi-Value Fields

Checkboxes and multi-selects can send a key many times. Use getlist to collect every chosen value into a list.

picks = request.form.getlist("topics")

Form vs Args at a Glance

Remember the split: request.args reads the URL query string, while request.form reads fields from the posted body.

values Merges Both Sources

Need either source without caring which? request.values combines args and form into one dict-like view for convenience.

token = request.values.get("token")

A Simple Signup View

Putting it together, a signup handler reads name and email from the form and responds once it has them in hand.

@app.route("/signup", methods=["POST"])
def signup():
    name = request.form["name"]
    return f"Welcome {name}"

Quick Check

Check your grip on reading form fields.

Recap: Form Fields

You pulled posted fields from request.form, used .get with defaults, handled multi-value inputs, and saw how it differs from args. 🙌

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

บทเรียน “ฟิลด์แบบฟอร์มผ่าน request.form” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “ฟิลด์แบบฟอร์มผ่าน request.form”

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

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

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

บทเรียน “ฟิลด์แบบฟอร์มผ่าน request.form” ใช้เวลานานแค่ไหน

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

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

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

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

  1. สตริงคำค้นผ่าน request.args
  2. ฟิลด์แบบฟอร์มผ่าน request.form
  3. เนื้อหา JSON ผ่าน request.get_json
  4. ส่วนหัว คุกกี้ และ IP ของไคลเอนต์
← กลับไปที่ Flask Academy