アプリのURLをincludeしてルートに名前を付ける
include()を使い、reverse()用にルートへ名前を付けます
「アプリのURLをincludeしてルートに名前を付ける」はCoddyKit上の無料Django Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはDjango Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Django Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Two Levels of URLs
Django splits routing into the project's urls.py and each app's own. The project includes app URLs to keep things tidy.
Why Apps Get Their Own urls.py
Giving each app its own URL file keeps routes reusable and self-contained. You can drop an app into any project cleanly.
Importing include
To delegate to an app, you use include. Import it alongside path from django.urls in the project file.
from django.urls import path, includeWiring Up include()
In the project urls.py, point a prefix at an app's URL module with include. All its routes hang under that prefix.
path('blog/', include('blog.urls'))How Prefixes Combine
The project prefix joins the app route. So blog/ plus an app route of post/ serves the full path blog/post/.
Naming a Route
Give each route a name so you never hardcode its URL. You refer to the route by this stable name everywhere else.
path('about/', views.about, name='about')Why Names Beat Hardcoding
If a URL changes, hardcoded links break. A named route lets you change the path once and every link still works.
reverse() in Python
Use reverse to build a URL from a route name in your view code. Django returns the current path for that name.
from django.urls import reverse
url = reverse('about')app_name and Namespaces
Set app_name in an app's urls.py to namespace its routes. Then names look like blog:detail, avoiding clashes between apps.
app_name = 'blog'Referencing Namespaced Routes
With a namespace set, you reference routes as namespace:name. This keeps blog:detail and shop:detail clearly separate.
reverse('blog:detail')The redirect Shortcut
The redirect helper also accepts a route name. Send users somewhere by name instead of a fragile literal path.
from django.shortcuts import redirect
return redirect('about')Quick Check
Recall what include() does in the project's urls.py.
Recap: Include and Name
Use include to mount app URLs under a prefix, and name routes so reverse and redirect stay solid when paths change. 🧭
よくある質問
「アプリのURLをincludeしてルートに名前を付ける」レッスンは無料ですか?
はい。「アプリのURLをincludeしてルートに名前を付ける」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Django Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Django Academyコースには全4レッスンが含まれています。
「アプリのURLをincludeしてルートに名前を付ける」で何を学びますか?
include()を使い、reverse()用にルートへ名前を付けます ブラウザで直接実行するハンズオンコードでDjango Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Django Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのDjango Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「アプリのURLをincludeしてルートに名前を付ける」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このDjango Academyレッスンでコードを書いて実行できますか?
はい。すべてのDjango Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 関数ベースビューを書く
- path()によるURLパターン
- URLパラメーターをキャプチャする
- アプリのURLをincludeしてルートに名前を付ける