staticフォルダーの規約
Flaskがデフォルトでアセットを探す場所を学びます
「staticフォルダーの規約」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlask Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flask Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Assets Need a Home
Your app needs CSS, JavaScript, and images. Flask gives them one default home: a folder named static next to your app. 📁
Where Flask Looks
By convention, Flask serves anything inside the static folder automatically, so you never write a route to hand out a stylesheet yourself.
The Folder Layout
Place static right beside your app.py file. Flask finds it relative to your application package without any extra setup.
myapp/
app.py
static/
style.css
templates/The Public URL Prefix
Files in that folder are reachable under the /static URL prefix. A file named style.css becomes available at /static/style.css in the browser.
Organize with Subfolders
You can nest folders inside static to stay tidy. The path on disk simply maps to the path in the URL, one to one.
static/
css/style.css
js/app.js
img/logo.pngURL Mirrors the Path
So static/css/style.css is served at /static/css/style.css. The browser path mirrors your folder structure exactly. 🔗
It Is Just a Default
The name static is a default, not a law. Flask uses it unless you tell it otherwise, which keeps every project predictable.
Rename the Folder if Needed
Pass static_folder when you create the app to point Flask at a differently named directory on disk.
from flask import Flask
app = Flask(__name__, static_folder="assets")Change the URL Prefix Too
You can also change the public prefix with static_url_path, so assets appear under /files instead of /static.
app = Flask(__name__, static_url_path="/files")Static Means Unchanging
These files are static because Flask sends them as-is, with no Python running. That is exactly why serving them is so fast.
Templates Are Different
Do not put HTML you render into static. Dynamic pages belong in templates, while static holds only finished, ready-to-send assets.
Quick Check
You drop logo.png into the static folder. Where can the browser reach it?
Recap: The static Folder
You learned the static convention: drop assets in that folder and Flask serves them under /static, fast and free. Next, link them cleanly. ✨
よくある質問
「staticフォルダーの規約」レッスンは無料ですか?
はい。「staticフォルダーの規約」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。
「staticフォルダーの規約」で何を学びますか?
Flaskがデフォルトでアセットを探す場所を学びます ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Flask Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「staticフォルダーの規約」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このFlask Academyレッスンでコードを書いて実行できますか?
はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。