0Pricing
Flask Academy · レッスン

型コンバーター:int、float、string、path

組み込みコンバーターでURLセグメントを制約します

「型コンバーター:int、float、string、path」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlask Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flask Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Strings Are Not Always Enough

Captured values default to strings, but /post/42 should give a number, not text. A converter tells Flask what type to expect in that segment.

Converter Syntax

Put the converter name before the variable with a colon. The pattern is <converter:name>, and Flask applies it before calling your view.

@app.route('/post/<int:post_id>')
def show(post_id):
    return str(post_id + 1)

The int Converter

Use int to capture whole numbers. /post/42 hands your view the integer 42, so you can do math without calling int() yourself. 🔢

@app.route('/page/<int:n>')
def page(n):
    return f'Page {n}'

int Rejects Non-Numbers

A converter also filters URLs. /page/abc will not match an int route, so Flask returns 404 instead of crashing inside your function.

The float Converter

Use float for decimal numbers like prices or ratings. /rate/4.5 gives you the float 4.5, ready for arithmetic right away.

@app.route('/rate/<float:score>')
def rate(score):
    return str(score * 2)

The string Converter

string is the default: it accepts any text but stops at a slash. You rarely write it explicitly since plain <name> already behaves this way.

@app.route('/tag/<string:label>')
def tag(label):
    return label

The path Converter

Use path when the value can contain slashes, like a folder route. Unlike string, it captures /docs/intro/setup as one whole value.

@app.route('/files/<path:filepath>')
def serve(filepath):
    return filepath

Pick the Right Tool

Match the converter to your data: int for ids, float for decimals, string for words, path for slashed routes. The right choice prevents bad input early.

Converters Validate for You

By rejecting mismatched URLs with a 404, converters act as a first line of validation. Your view only runs when the type already fits.

Combine in One Route

You can mix converters across segments. Here an int id and a string tab live in the same path, each typed independently.

@app.route('/user/<int:uid>/<tab>')
def view(uid, tab):
    return f'{uid}:{tab}'

Negatives and Edge Cases

The built-in int converter matches only non-negative whole numbers by default. For minus signs or stricter rules, you would write a custom converter later.

Quick Check

Which converter lets a captured value include slashes, like docs/intro/setup?

Recap: You Typed Your URLs

Converters give captured values a type and reject bad input with a 404. Remember int, float, string, and path. Next: building URLs safely. 🧭

よくある質問

「型コンバーター:int、float、string、path」レッスンは無料ですか?

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

「型コンバーター:int、float、string、path」で何を学びますか?

組み込みコンバーターでURLセグメントを制約します ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「型コンバーター:int、float、string、path」レッスンにはどのくらい時間がかかりますか?

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

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

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

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

  1. パスから変数を取得する
  2. 型コンバーター:int、float、string、path
  3. url_forでURLを構築する
  4. 末尾のスラッシュとリダイレクトの動作
← Flask Academyに戻る