แยกตรรกะตาม request.method
แสดงแบบฟอร์มเมื่อเป็น GET และประมวลผลเมื่อเป็น POST
แยกตรรกะตาม request.method เป็นบทเรียน Flask Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Flask Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
One Route, Two Jobs
A route that accepts both GET and POST often needs to behave differently for each. You handle that by checking which verb arrived.
The request Object
Flask gives you a global request object that describes the incoming call. Import it once and use it inside any view.
from flask import requestRead request.method
The attribute request.method holds the verb of the current call as a string like "GET" or "POST".
if request.method == "POST":
print("handling a form")Branch on the Verb
Wrap your logic in an if that checks request.method. One branch shows a form, the other processes the submitted data.
The Classic Form Pattern
The most common shape is: on GET render the empty form, and on POST read the answers and act on them.
@app.route("/contact", methods=["GET", "POST"])
def contact():
if request.method == "POST":
return "saved"
return "show form"GET Should Not Change Data
Keep the GET branch read-only. A GET request must never create or modify records, since browsers may repeat it freely.
POST Does the Work
Put writes, saves, and side effects in the POST branch. That keeps mutations tied to a deliberate submission.
Compare Strings Exactly
Always compare against the exact uppercase value. request.method is "POST", never "post", so a lowercase check will silently fail.
An else for the Fallthrough
Treat the GET case as the natural else. Return the form view when the method is not POST so the page always renders.
Skip Checks Flask Handles
You never test for a verb you did not allow. Flask blocks unlisted methods before your code runs, so only listed verbs arrive.
Keep Branches Small
If each branch grows large, call helper functions from the if. The view stays a clean dispatcher between GET and POST.
Quick Check
Pick the branch that should change data.
Recap
Use request.method to branch: render the form on GET, process and save on POST. Keep GET read-only and POST for changes. Great work! 🙌
คำถามที่พบบ่อย
บทเรียน “แยกตรรกะตาม request.method” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “แยกตรรกะตาม request.method” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flask Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “แยกตรรกะตาม request.method”
แสดงแบบฟอร์มเมื่อเป็น GET และประมวลผลเมื่อเป็น POST คุณปฏิบัติ Flask Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flask Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flask Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “แยกตรรกะตาม request.method” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Flask Academy นี้ได้ไหม
ได้ บทเรียน Flask Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ประกาศเมธอดที่อนุญาตในเส้นทาง
- แยกตรรกะตาม request.method
- อ่านข้อมูล POST จากเนื้อหาคำขอ
- อธิบาย 405 Method Not Allowed