current_appとgオブジェクト
アクティブなアプリとリクエスト単位のストレージにアクセスします
「current_appとgオブジェクト」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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_appWhy 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オブジェクト」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。
「current_appとgオブジェクト」で何を学びますか?
アクティブなアプリとリクエスト単位のストレージにアクセスします ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Flask Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「current_appとgオブジェクト」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このFlask Academyレッスンでコードを書いて実行できますか?
はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- アプリコンテキストとリクエストコンテキスト
- current_appとgオブジェクト
- コンテキストローカルとスレッドセーフティ
- スクリプトとシェルでコンテキストをプッシュする