0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[備忘録]includeについて

Posted at

はじめに

初めまして。独学で、プログラミングの学習を行っているえむしたと申します。
今回は、Djangoのincludeについての備忘録です
拙い記事ではありますが、読んでいただけると幸いです

includeとは

簡単に言うと、URLパターンの再利用と整理を助ける関数である
といっても意味が分からなかったので、自分なりに深堀しました

test_project/urls.py
from django.contrib import admin 
from django.urls import path, include #includeをインポート


urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('test.urls')), #'test.urls'のURLパターンを組み込む
]

test_project/urls.pyは、プロジェクト全体のURLを管理する

test/urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'), #'test_project/urls'からインポートされる
]

test/urls.pyはプロジェクト内の各アプリのURLを管理する

これにより、test_project/urls.pyはユーザー認証機能や、管理画面のURLなど、test/urls.pyは単体アプリのURLをそれぞれの役割で管理できるようになる

最初は、分ける意味あるのか?と思いましたが、こうやって説明されると"整理"の意味が自分の中で理解できた気がします

まだまだ実践レベルの理解度ではありませんが、何となくすっきりした気がします

まとめ

includeは、それぞれの役割を持ったURLパターンを繋げられる関数である事がわかりました
まだまだ、奥は深いですね・・勉強がんばr

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?