0Pricing
Django Academy · レッスン

path()によるURLパターン

urls.pyでURLをビューに接続します

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

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

Why URLs Need Mapping

Django needs to know which view handles which address. The URLconf is the map that connects a URL to a view.

The urls.py File

Your URL map lives in urls.py. It holds a list called urlpatterns that Django checks top to bottom for a match.

Importing path

The main tool for routing is the path function. Import it from django.urls at the top of the file.

from django.urls import path

Anatomy of path()

The path function takes a route string and the view to call. You list these inside the urlpatterns list.

path('about/', views.about)

A Complete urlpatterns

Here is a working map. The urlpatterns list ties the about/ route to the about view function.

from django.urls import path
from . import views

urlpatterns = [
    path('about/', views.about),
]

Pass the View, Don't Call It

Write views.about without parentheses. Django calls the view for you when the route matches a request.

The Empty Route

An empty string route, '', matches your site's root. It is how you point the home page at a view.

path('', views.home)

Trailing Slashes Matter

Django routes usually end with a slash, like about/. This is a strong convention, so keep it consistent across routes.

Order Decides the Match

Django reads urlpatterns top to bottom and stops at the first match. Put more specific routes above broader ones.

Importing Your Views

Bring your views into urls.py first. The import line lets you reference each view by name in path calls.

from . import views

What Happens on No Match

If no pattern matches the request, Django returns a 404 error. That is your signal to add or fix a route.

Quick Check

Think about how you reference a view inside path().

Recap: You Mapped a URL

In urls.py you import path, list routes in urlpatterns, and point each one at a view. Order and trailing slashes matter. 🔗

よくある質問

「path()によるURLパターン」レッスンは無料ですか?

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

「path()によるURLパターン」で何を学びますか?

urls.pyでURLをビューに接続します ブラウザで直接実行するハンズオンコードでDjango Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「path()によるURLパターン」レッスンにはどのくらい時間がかかりますか?

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

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

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

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

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