0
0
Djangoframework~10 mins

Including app URLs in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Including app URLs
Start: Project urls.py
Import include()
Define urlpatterns
Use path() with include('app.urls')
Request to app URL
Django routes to app.urls
App urls.py handles request
Return response
The main project urls.py file includes app-specific urls.py files using include(), so requests to app URLs are routed to the app's URL patterns.
Execution Sample
Django
from django.urls import path, include

urlpatterns = [
    path('blog/', include('blog.urls')),
]
This code includes the URL patterns from the 'blog' app under the 'blog/' path prefix.
Execution Table
StepActionCode LineResult
1Start project urls.pyfrom django.urls import path, includeinclude() function imported
2Define urlpatterns listurlpatterns = [path('blog/', include('blog.urls'))]urlpatterns contains path with include
3Incoming request to /blog/post/1Request URL: /blog/post/1Matches 'blog/' prefix
4Django calls include('blog.urls')include('blog.urls')Loads blog/urls.py patterns
5blog.urls.py matches 'post/1'path('post/<int:id>/', views.post_detail)Matched view: post_detail with id=1
6View returns responseviews.post_detail(request, id=1)Response sent to client
💡 Request handled by blog app urls.py after matching 'blog/' prefix and app URL patterns
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 5Final
urlpatterns[][path('blog/', include('blog.urls'))][path('blog/', include('blog.urls'))][path('blog/', include('blog.urls'))][path('blog/', include('blog.urls'))]
request_path'''''post/1''post/1''post/1'
matched_viewNoneNoneNonepost_detailpost_detail
Key Moments - 2 Insights
Why does Django look inside blog.urls after matching 'blog/' in the project urls.py?
Because the path uses include('blog.urls'), Django delegates routing to the blog app's urls.py for any URL starting with 'blog/'. This is shown in execution_table step 4.
What happens if the request URL does not start with 'blog/'?
Django will not use the blog.urls patterns and will continue checking other urlpatterns in the project urls.py or return 404 if no match is found. This is implied by the matching in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'request_path' after step 4?
A'/blog/post/1'
B''
C'post/1'
DNone
💡 Hint
Check variable_tracker row for 'request_path' after step 4
At which step does Django load the app's urls.py patterns?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look at execution_table action descriptions for when include('blog.urls') is called
If the project urls.py did not include path('blog/', include('blog.urls')), what would happen to a request to '/blog/post/1'?
ADjango would return a 404 error
BIt would be routed to blog.urls anyway
CIt would match another app automatically
DThe request would be redirected to '/'
💡 Hint
Consider what happens if no matching path is found in urlpatterns
Concept Snapshot
Including app URLs in Django:
- Use include() in project urls.py to add app urls
- Syntax: path('prefix/', include('app.urls'))
- Requests matching prefix go to app.urls
- App urls.py defines detailed routes
- Keeps project urls.py clean and modular
Full Transcript
In Django, the main project urls.py file includes URL patterns from apps using the include() function. This means when a request URL starts with a prefix like 'blog/', Django passes routing to the blog app's urls.py. The app urls.py then matches the rest of the URL and calls the appropriate view. This modular approach helps organize URLs clearly. The execution trace shows importing include(), defining urlpatterns with include(), matching a request URL, delegating to app urls.py, and finally returning a response from the matched view.