0Pricing
Django Academy · レッスン

URLパラメーターをキャプチャする

intやslugなどのパスコンバーターをビューで読み取ります

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

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

Why Capture Parameters

Many pages depend on a value in the URL, like a product id. Django lets you capture that value and pass it straight to your view.

Angle Bracket Syntax

You capture a piece of the URL with angle brackets. Whatever is inside becomes a named argument for your view.

path('post/<int:id>/', views.detail)

Converters Set the Type

The word before the colon is a converter. It both matches a pattern and converts the captured text to the right Python type.

The int Converter

The int converter matches digits and hands your view a real integer, not a string. Perfect for numeric ids.

path('post/<int:id>/', views.detail)

The slug Converter

The slug converter matches letters, numbers, hyphens, and underscores. It is ideal for readable, SEO-friendly URLs.

path('blog/<slug:title>/', views.post)

The View Receives the Value

The captured name becomes a parameter in your view, right after request. The names must match exactly.

def detail(request, id):
    return HttpResponse('Post ' + str(id))

Names Must Match

The name in the URL and the view argument must be identical. If the URL says id, your view must accept id too.

More Built-in Converters

Beyond int and slug, Django ships str, uuid, and path converters. Each matches a different shape of value.

str Is the Default

If you skip the converter, Django uses str. It matches any text except a slash, which keeps segments separate.

path('user/<username>/', views.profile)

Capturing Multiple Values

You can capture several values in one route. Each becomes its own keyword argument in the view, in order.

path('<int:year>/<int:month>/', views.archive)

Wrong Type, No Match

If a URL value does not fit the converter, the route simply does not match. Django moves on or returns a 404.

Quick Check

Consider which converter fits a numeric id.

Recap: Dynamic URLs

Use angle brackets with a converter to capture URL values. The view receives them by name, typed and ready to use. ✨

よくある質問

「URLパラメーターをキャプチャする」レッスンは無料ですか?

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

「URLパラメーターをキャプチャする」で何を学びますか?

intやslugなどのパスコンバーターをビューで読み取ります ブラウザで直接実行するハンズオンコードでDjango Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「URLパラメーターをキャプチャする」レッスンにはどのくらい時間がかかりますか?

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

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

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

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

  1. 関数ベースビューを書く
  2. path()によるURLパターン
  3. URLパラメーターをキャプチャする
  4. アプリのURLをincludeしてルートに名前を付ける
← Django Academyに戻る