url_for staticでアセットにリンクする
パスをハードコードせずCSSとJSを参照します
「url_for staticでアセットにリンクする」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlask Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flask Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Stop Hardcoding Paths
Writing /static/style.css by hand in every template feels easy, but it is fragile. Flask gives you url_for to build asset links for you. 🔧
The static Endpoint
Flask registers a built-in endpoint named static for your assets. You ask url_for for it instead of typing the raw path yourself.
Pass the Filename
Call url_for with the endpoint and a filename argument. Flask returns the correct URL pointing into your static folder.
url_for("static", filename="style.css")
# -> /static/style.cssUse It in a Template
Inside a Jinja2 template you wrap the call in double braces. The link tag then receives a generated, always-correct href.
<link rel="stylesheet"
href="{{ url_for('static', filename='style.css') }}">Subfolders Work Too
Put the subpath right inside filename. Slashes in the value map straight onto folders within static.
url_for("static", filename="css/style.css")
# -> /static/css/style.cssWhy Bother
If you ever change static_url_path, every url_for link updates itself. Hardcoded paths would all silently break instead.
Correct Prefixes for Free
When your app runs under a sub-path on a server, url_for adds the right prefix automatically, so links keep working everywhere.
Link Your JavaScript
The same call wires up scripts. Point the script tag at url_for and your JS loads from static without a hardcoded path.
<script src="{{ url_for('static', filename='js/app.js') }}"></script>Images the Same Way
Pictures follow the identical pattern. Feed the filename to url_for and drop the result into the img tag source.
<img src="{{ url_for('static', filename='img/logo.png') }}">Call It in Python Too
url_for is not template-only. Inside a view you can call url_for to compute an asset URL and return or log it.
from flask import url_for
link = url_for("static", filename="style.css")One Habit to Keep
Make it a rule: every asset link goes through url_for. Future renames, prefixes, and cache busting then just work for you. ✅
Quick Check
You want a clean link to css/style.css inside static. Which call is right?
Recap: Linking Assets
You now use url_for with the static endpoint to build every asset link, so paths stay correct no matter how config changes. 🎯
よくある質問
「url_for staticでアセットにリンクする」レッスンは無料ですか?
はい。「url_for staticでアセットにリンクする」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。
「url_for staticでアセットにリンクする」で何を学びますか?
パスをハードコードせずCSSとJSを参照します ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Flask Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「url_for staticでアセットにリンクする」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このFlask Academyレッスンでコードを書いて実行できますか?
はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- staticフォルダーの規約
- url_for staticでアセットにリンクする
- ファイルバージョンによるキャッシュ破棄
- 本番環境における静的ファイルの現実