Blueprintを作成して登録する
Blueprintを定義し、アプリに接続します
「Blueprintを作成して登録する」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlask Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flask Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Two Steps to a Blueprint
Working with a blueprint is two moves: first you define it in its own module, then you register it on the app. Let us walk through both.
Import the Blueprint Class
Everything starts with one import. The Blueprint class comes straight from flask, just like the Flask app object does.
from flask import BlueprintCreate the Blueprint Object
You build a blueprint by giving it a name and import_name. The name uniquely identifies this blueprint inside your whole app.
blog = Blueprint("blog", __name__)What __name__ Does
That second argument, __name__, tells the blueprint where it lives so Flask can later find its templates and static files.
Routes on the Blueprint
Inside the module you attach views with @blog.route instead of @app.route. Same idea, but bound to the blueprint.
@blog.route("/posts")
def posts():
return "All posts"Still Dormant for Now
Defining routes is not enough. Until you register the blueprint, none of its URLs respond. The blueprint is inert on its own.
Import It in the App
Over in your app setup, you import the blueprint object from its module so the app can take ownership of it.
from blog.routes import blogRegister on the App
One call wires everything up. register_blueprint copies the blueprint routes onto the app and makes them live.
app.register_blueprint(blog)Order of Operations
Create the app, import the blueprint, then register it. Get this sequence right and your feature routes spring to life. ✅
Endpoint Names Get Namespaced
A view named posts inside the blog blueprint is referenced as blog.posts. The blueprint name becomes a tidy prefix for endpoints.
Avoiding Circular Imports
Keep the blueprint in its own file and import it where you build the app. This structure sidesteps painful circular import errors.
Quick Check
Which call actually makes a blueprint active?
Recap
Define a Blueprint with a name and __name__, add routes with @bp.route, then call register_blueprint on the app to make them live. 🎉
よくある質問
「Blueprintを作成して登録する」レッスンは無料ですか?
はい。「Blueprintを作成して登録する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。
「Blueprintを作成して登録する」で何を学びますか?
Blueprintを定義し、アプリに接続します ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Flask Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「Blueprintを作成して登録する」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このFlask Academyレッスンでコードを書いて実行できますか?
はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 巨大な1ファイル構成よりBlueprintが優れている理由
- Blueprintを作成して登録する
- URLプレフィックスとBlueprintのurl_for
- Blueprint専用のテンプレートと静的ファイル