0Pricing
Flask Academy · レッスン

アプリコンテキストとリクエストコンテキスト

2つのコンテキストと、それぞれに存在するものを学びます

「アプリコンテキストとリクエストコンテキスト」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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. 🎉

よくある質問

「アプリコンテキストとリクエストコンテキスト」レッスンは無料ですか?

はい。「アプリコンテキストとリクエストコンテキスト」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。

「アプリコンテキストとリクエストコンテキスト」で何を学びますか?

2つのコンテキストと、それぞれに存在するものを学びます ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Flask Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「アプリコンテキストとリクエストコンテキスト」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このFlask Academyレッスンでコードを書いて実行できますか?

はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. アプリコンテキストとリクエストコンテキスト
  2. current_appとgオブジェクト
  3. コンテキストローカルとスレッドセーフティ
  4. スクリプトとシェルでコンテキストをプッシュする
← Flask Academyに戻る