パスから変数を取得する
ルートで山かっこ形式のパラメーターを使います
「パスから変数を取得する」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlask Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flask Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Static Routes Hit a Wall
A fixed path like /user/alice only works for one person. To serve any name, you need a route that captures part of the URL as data.
Angle Brackets Capture Values
Wrap a path segment in angle brackets to turn it into a variable. Flask grabs whatever the user types there and hands it to your view as a parameter. 🎯
@app.route('/user/<name>')
def profile(name):
return f'Hello, {name}!'The Name Must Match
The word inside the brackets becomes the function argument. The name in the URL and the parameter in your view function must be spelled identically.
@app.route('/city/<place>')
def show(place):
return placeValues Arrive as Strings
By default a captured value is always a Python string. Visiting /user/42 gives you the text "42", not the number 42.
@app.route('/id/<value>')
def show(value):
return type(value).__name__Capture More Than One
A single route can capture several segments. Each set of angle brackets becomes its own argument in the view function, in order.
@app.route('/<category>/<item>')
def show(category, item):
return f'{category}: {item}'Mix Fixed and Dynamic Parts
You can blend static text with captured pieces. Only the bracketed segment is dynamic; the rest of the path stays fixed and literal.
@app.route('/posts/<slug>/edit')
def edit(slug):
return slugUse the Value in Your Logic
Once captured, the variable is just normal Python. Look it up in a database, format it, or branch on it like any other value.
@app.route('/user/<name>')
def hi(name):
return f'Welcome back, {name.title()}'Each Request Is Independent
Every visit fills the captured variable fresh. One user hitting /user/sam and another hitting /user/lee get separate, isolated requests.
Slashes Split Segments
A normal capture stops at the next slash. So name in /user/<name> will not include any /, since the slash marks the end of that segment.
Naming Your Variables Well
Pick clear names that describe the data, like <username> or <product_id>. Good names make routes self-documenting and easier to maintain.
A Tiny Real Example
Captured variables power friendly URLs like /greet/Maria. The view simply uses the value to build a personalized response for each visitor.
@app.route('/greet/<who>')
def greet(who):
return f'Nice to meet you, {who}'Quick Check
You wrote /user/<name> with def profile(name). What gets passed to name?
Recap: You Captured the Path
Angle brackets turn part of a URL into a function argument. Captured values arrive as strings, and you can grab several at once. Next: typing them. 🚀
よくある質問
「パスから変数を取得する」レッスンは無料ですか?
はい。「パスから変数を取得する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。
「パスから変数を取得する」で何を学びますか?
ルートで山かっこ形式のパラメーターを使います ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Flask Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「パスから変数を取得する」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このFlask Academyレッスンでコードを書いて実行できますか?
はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。