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

ส่งสตริง HTML และรหัสสถานะ

ส่งเนื้อหาหลายรูปแบบและกำหนดสถานะ HTTP

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

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

Returning a Plain String

The simplest response is a plain string. Flask sends it straight to the browser as the page body with a 200 status.

@app.route("/")
def home():
    return "Hello"

Strings Can Hold HTML

Your string can contain real HTML tags. The browser parses them, so returning a heading renders as a styled title, not as raw text.

return "<h1>Welcome</h1>"

Inline HTML Gets Messy

Writing big pages as one giant string gets unreadable fast. It works for tiny snippets, but real pages belong in template files later.

The Default Status

When you return just a body, Flask assumes a 200 status, meaning OK. You only set a code when the result is not a plain success.

Return a Status Code

Return a tuple of body and number to set the status. Here a missing item answers with a 404 instead of the default 200.

return "Not found", 404

Common Success Codes

Use 201 when you create something new and 200 for a normal read. The right code tells clients exactly what happened.

return "Created", 201

Common Error Codes

Use 400 for bad input, 404 for missing pages, and 500 when your own code crashes. Each number has a clear, agreed meaning.

Add Headers Too

Extend the tuple with a third part, a dict of headers, when you need to control things like the content type of the reply.

return "OK", 200, {"X-App": "demo"}

make_response for Control

For finer control, build a make_response object. You can set its status and headers step by step before returning it.

resp = make_response("Hi")
resp.status_code = 202

Strings Are Auto-Wrapped

Even a bare string gets wrapped in a full response behind the scenes. Flask handles the boilerplate so your views stay short.

Pick the Honest Code

Always return a truthful status. Sending 200 for an error confuses clients and hides real bugs from anyone using your app.

Quick Check

Pick the response that sets a custom status.

Recap

You can return plain text, HTML, or a tuple with a status and headers. Honest status codes keep your responses clear and trustworthy. ✅

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

บทเรียน “ส่งสตริง HTML และรหัสสถานะ” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “ส่งสตริง HTML และรหัสสถานะ”

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

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

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

บทเรียน “ส่งสตริง HTML และรหัสสถานะ” ใช้เวลานานแค่ไหน

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

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

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

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

  1. ตัวตกแต่ง @app.route
  2. คำขอกลายเป็นการตอบกลับได้อย่างไร
  3. ส่งสตริง HTML และรหัสสถานะ
  4. หลายเส้นทางในแอปเดียว
← กลับไปที่ Flask Academy