Including app URLs helps organize your website by keeping each app's web addresses separate and easy to manage.
Including app URLs in Django
from django.urls import include, path urlpatterns = [ path('appname/', include('appname.urls')), ]
The include() function tells Django to look inside the app's urls.py for more URL patterns.
The string before include() is the URL prefix for all URLs in that app.
blog app under the URL prefix /blog/.path('blog/', include('blog.urls'))
shop app under the URL prefix /shop/.path('shop/', include('shop.urls'))
main app at the root URL (no prefix).path('', include('main.urls'))
This example shows how the main urlpatterns includes URLs from two apps: blog and shop. Each app has its own urls.py and views. Visiting /blog/ shows the blog welcome message, and /shop/ shows the shop welcome message.
from django.urls import path, include urlpatterns = [ path('blog/', include('blog.urls')), path('shop/', include('shop.urls')), ] # blog/urls.py from django.urls import path from . import views urlpatterns = [ path('', views.index, name='blog-index'), ] # shop/urls.py from django.urls import path from . import views urlpatterns = [ path('', views.home, name='shop-home'), ] # views.py for blog from django.http import HttpResponse def index(request): return HttpResponse('Welcome to the Blog!') # views.py for shop from django.http import HttpResponse def home(request): return HttpResponse('Welcome to the Shop!')
Always create a urls.py file inside each app to define its URL patterns.
Using include() keeps your project URLs clean and easier to read.
Remember to import include from django.urls in your main urls.py.
Including app URLs helps organize your project by separating URL patterns per app.
Use path('prefix/', include('app.urls')) to add app URLs under a URL prefix.
This makes your project easier to maintain and scale.