ผลักบริบทในสคริปต์และเชลล์
เรียกใช้โค้ดนอกคำขอด้วย app_context
ผลักบริบทในสคริปต์และเชลล์ เป็นบทเรียน Flask Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Flask Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Outside the Web
Sometimes you run Flask code with no request, like a script that seeds the database. There is no context, so the usual proxies fail.
The Telltale Error
Run such code and Flask raises RuntimeError: working outside of application context. It is telling you no app context is active.
Push It Yourself
Fix it by pushing one with app.app_context(). Wrap your code in a with block and current_app starts working again.
with app.app_context():
print(current_app.name)What the with Block Does
Entering the with block pushes the context, and leaving it pops the context. You never have to clean up by hand.
Now Extensions Work
Inside the block, extensions bound to the app behave normally. You can run db.create_all() or query models from a plain script.
with app.app_context():
db.create_all()Need a Fake Request?
To exercise code that reads request, push a test request context with test_request_context instead of an app context.
with app.test_request_context("/hello"):
print(request.path)The Flask Shell
The flask shell command opens a Python shell with an app context already pushed, so you can poke at your app interactively.
flask shellCLI Commands Too
Custom flask commands you register also run inside an app context automatically, so seed and admin scripts just work.
@app.cli.command()
def seed():
db.create_all()Match the Right Context
Pick by need: use app_context when you only touch the app or database, and a request context when your code reads request data.
Keep It Scoped
Push contexts in a tight with block, not for the whole script. Smaller scope means clearer code and predictable teardown.
Why This Matters
Pushing contexts lets you reuse app logic in migrations, cron jobs, and tests, not just inside live web requests.
Quick Check
Let us nail the fix for the context error.
Recap
No request? Push one. Use app_context for app and database work, test_request_context for request data, and let flask shell handle it for you. 🎉
คำถามที่พบบ่อย
บทเรียน “ผลักบริบทในสคริปต์และเชลล์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ผลักบริบทในสคริปต์และเชลล์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flask Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ผลักบริบทในสคริปต์และเชลล์”
เรียกใช้โค้ดนอกคำขอด้วย app_context คุณปฏิบัติ Flask Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flask Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flask Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “ผลักบริบทในสคริปต์และเชลล์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Flask Academy นี้ได้ไหม
ได้ บทเรียน Flask Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- บริบทแอปเทียบกับบริบทคำขอ
- current_app และออบเจ็กต์ g
- ตัวแปรเฉพาะบริบทและความปลอดภัยของเธรด
- ผลักบริบทในสคริปต์และเชลล์