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

current_app และออบเจ็กต์ g

เข้าถึงแอปที่กำลังทำงานและพื้นที่จัดเก็บต่อคำขอ

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

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

Meet current_app

current_app is a proxy that always points to the app handling the work right now. Import it whenever you need the active application.

from flask import current_app

Why Not Import app?

Importing your app object directly causes circular imports as projects grow. current_app sidesteps that by resolving the app at runtime.

Reading Config

A common use is reading settings: current_app.config gives you the active app's configuration without importing the app module.

key = current_app.config["SECRET_KEY"]

It Needs a Context

current_app only works while an app context is active. Inside a view that is automatic, since the request pushed one for you.

Meet the g Object

g is a simple scratchpad for one request. Attach anything to it and read it back later in the same request.

from flask import g
g.user = "alice"

g Is Per Request

The g object is reset for every request. Data you stash on it never leaks into another visitor's request.

A Typical g Use

Use g to share work across functions in one request, like caching the current user so you fetch it from the database only once.

if "db" not in g:
    g.db = connect()

g Is Not Storage

Do not treat g as a database. It forgets everything the moment the request ends, so use it only for that request's lifetime.

g Lives in the App Context

Surprisingly, g belongs to the app context, not the request context. In practice both exist during a request, so it just works.

Cleaning Up With g

Pair g with teardown handlers to close what you opened, like closing a database connection after the response is sent.

@app.teardown_appcontext
def close(exc):
    g.pop("db", None)

current_app and g Together

They pair nicely: read settings from current_app, then stash request-specific results on g so the rest of the request can reuse them.

Quick Check

Let us pin down what g is for.

Recap

Two handy proxies: current_app finds the active app and its config, while g is a per-request scratchpad you wipe clean each time. 🎉

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

บทเรียน “current_app และออบเจ็กต์ g” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “current_app และออบเจ็กต์ g”

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

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

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

บทเรียน “current_app และออบเจ็กต์ g” ใช้เวลานานแค่ไหน

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

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

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

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

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