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

บริบทแอปเทียบกับบริบทคำขอ

สองบริบทและสิ่งที่อยู่ในแต่ละบริบท

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

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

Two Kinds of Context

Flask runs your code inside a context, a temporary bubble holding the data your view needs. There are two flavors: an app context and a request context.

The App Context

The application context tracks which app is active right now. It exists whenever Flask is doing work, even outside a real web request.

The Request Context

The request context wraps a single incoming request. It holds the URL, headers, and form data for that one visitor only.

Request Pushes App Too

When a request arrives, Flask pushes a request context, and an app context rides along automatically. So inside a view, both are always active.

Who Lives in the App Context

The app context gives you current_app and the g object. Use them for config, the database handle, and other per-request shared data.

from flask import current_app
print(current_app.name)

Who Lives in the Request Context

The request context gives you request and session. These describe the visitor: their path, query string, posted form, and cookies.

from flask import request
path = request.path

Why Split Them?

Splitting contexts means app-level work, like a startup script, can use current_app without pretending a fake web request exists.

Outside Any Context

Touch request with no active context and Flask raises a runtime error. The proxy has nothing real to point at yet.

Contexts Are Stacked

Flask keeps contexts on a stack. Each is pushed when work starts and popped when it ends, so the right data is always on top.

Lifespan of a Context

A request context lives only for that one request. Once the response is sent, Flask pops both contexts and they vanish.

Mental Model

Think of it this way: the app context answers which app, and the request context answers which visitor. Inside a view you have both at once.

Quick Check

Let us sort the two contexts apart.

Recap

Two bubbles, two jobs: the app context says which app, the request context says which visitor. A request pushes both, so views always have each. 🎉

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

บทเรียน “บริบทแอปเทียบกับบริบทคำขอ” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “บริบทแอปเทียบกับบริบทคำขอ”

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

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

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

บทเรียน “บริบทแอปเทียบกับบริบทคำขอ” ใช้เวลานานแค่ไหน

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

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

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

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

  1. บริบทแอปเทียบกับบริบทคำขอ
  2. current_app และออบเจ็กต์ g
  3. ตัวแปรเฉพาะบริบทและความปลอดภัยของเธรด
  4. ผลักบริบทในสคริปต์และเชลล์
← กลับไปที่ Flask Academy